To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / python-intro.py @ 5:f358a45c148e
History | View | Annotate | Download (852 Bytes)
| 1 |
def is_data(line): |
|---|---|
| 2 |
""" Returns true if the line has data
|
| 3 |
and false otherwise (header, comments, etc)"""
|
| 4 |
|
| 5 |
if line.startswith('#'): |
| 6 |
# skip comments
|
| 7 |
return False |
| 8 |
elif line.startswith('D'): |
| 9 |
# skip title row
|
| 10 |
return False |
| 11 |
else:
|
| 12 |
return True |
| 13 |
|
| 14 |
def count_marlins(data): |
| 15 |
"""Receives a line of data and returns the count of Marlins in that line (zero otherwise)"""
|
| 16 |
|
| 17 |
date, species, count = data.split(",")
|
| 18 |
marlins = 0
|
| 19 |
if species == "marlin": |
| 20 |
marlins = count |
| 21 |
return int(marlins) |
| 22 |
|
| 23 |
|
| 24 |
# script starts here...
|
| 25 |
source = open('data.txt', 'r') |
| 26 |
|
| 27 |
number_of_marlins = 0
|
| 28 |
|
| 29 |
# count number of data records
|
| 30 |
for line in source: |
| 31 |
if is_data(line) == True: |
| 32 |
number_of_marlins = number_of_marlins + count_marlins(line) |
| 33 |
|
| 34 |
source.close() |
| 35 |
|
| 36 |
print "Total number of marlins:", number_of_marlins |