To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / python-intro.py

History | View | Annotate | Download (896 Bytes)

1 4:79ef88975054 luis
def is_data(line):
2
    """ Returns true if the line has data
3
    and false otherwise (header, comments, etc)"""
4
5 6:4ec6aa30a1f8 luis
    answer = True
6
7 4:79ef88975054 luis
    if line.startswith('#'):
8
        # skip comments
9 6:4ec6aa30a1f8 luis
        answer = False
10 4:79ef88975054 luis
    elif line.startswith('D'):
11
        # skip title row
12 6:4ec6aa30a1f8 luis
        answer = False
13 4:79ef88975054 luis
    else:
14 6:4ec6aa30a1f8 luis
        answer = True
15
    return answer
16
17 4:79ef88975054 luis
18 5:f358a45c148e luis
def count_marlins(data):
19
    """Receives a line of data and returns the count of Marlins in that line (zero otherwise)"""
20
21
    date, species, count = data.split(",")
22
    marlins = 0
23
    if species == "marlin":
24
        marlins = count
25
    return int(marlins)
26
27
28 4:79ef88975054 luis
# script starts here...
29 0:15b82ccc064b luis
source = open('data.txt', 'r')
30 2:c6c0578924fa luis
31 5:f358a45c148e luis
number_of_marlins = 0
32 1:d6b2e34ab8c0 luis
33
# count number of data records
34 0:15b82ccc064b luis
for line in source:
35 4:79ef88975054 luis
    if is_data(line) == True:
36 5:f358a45c148e luis
        number_of_marlins = number_of_marlins + count_marlins(line)
37 3:8a8bccdf4e9c luis
38 0:15b82ccc064b luis
source.close()
39 1:d6b2e34ab8c0 luis
40 5:f358a45c148e luis
print "Total number of marlins:", number_of_marlins