This is an old revision of the document!


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")

 
bird.1200578246.txt.gz · Last modified: 2008/01/17 08:57 by jtk
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki