A slight variation of what was given in class…
import string
def main(search):
total = 0
c = 0
infile = open("birdlist48.csv")
infile.readline()
infile.readline()
for line in infile:
# print line
fields = string.split(line, ",")
if fields[0]:
order = string.capwords(fields[0])
name = fields[6]
if name:
total = total + 1
if name and string.find(string.lower(name), search) >= 0:
c = c + 1
genus = fields[4]
species = fields[5]
print "%-30s (%s %s, order %s)" % (name, genus, species, order)
print "%d birds, of which %d match %s" % (total, c, search)
main("hawk")
The version developed in class…
import string
def main(search):
total = 0
c = 0
infile = open("birdlist48.csv")
infile.readline()
infile.readline()
for line in infile:
# print line
fields = string.split(line, ",")
if fields[0]:
order = string.capwords(fields[0])
name = fields[6]
if name:
total = total + 1
if name and string.find(string.lower(name), search) >= 0:
c = c + 1
genus = fields[4]
species = fields[5]
print "%-30s (%s %s, order %s)" % (name, genus, species, order)
print "%d birds, of which %d match %s" % (total, c, search)
main("hawk")