Hi all,

I'm working on this problem where I want to get a dictionary list of all tree species with the count. That part has been worked out. With in the for loop for tree species, I also want to get the sum of DBH (diameter breast height) for each tree species. The code is reading a text file. I'm getting a type error...and then it gives me one of the DBHs. I want an entry value for each key, but maybe it's getting caught on a duplicate key...not sure?

The code is also set up so that if it is the first entry of a tree species, then it should add to the list in the else statement.

The only thing I'm caught on is this key error. Can anyone help me see where I've gone wrong?

Thanks for any help.

#Currently, this script reads a text file and prints the count for each species.
#Each row in the text file has this format: Block   Plot    Species     DBH
#Columns are tab separated.
#When completed, this script will also find the average DBH of each species in the given text file.

import string, os.path

filepath = "C:\\temp\\rdu_forest.txt"

#Open the text file
#Read the text file
myfile.readline() #read the field name line
row = myfile.readline()
count = 0
while row:
    myline = row.split('\t') #Creat a list of the values in this row.  Columns are tab separated.
    #Reads a file with columns: Block Plot  Species DBH MerchHeight
    #rstrip removes white space from the right side
    count = count + 1
    row = myfile.readline()


for row in data:  # for each row
    # create or update a dictionary entry with the current count for that species
    species = row[2]  #Species is the third entry in the file
    DBH = row[3] #DBH is the fourth entry in the file 
    if mydict.has_key(species):  #if a dictionary entry already exists for this species
        #Update dict for this species
        cur_entry = mydict[species]
        cur_entry = int(cur_entry)+1
        mydict[species] = cur_entry
        #print mydict
        update mydict2 
        cur_area = mydict2[DBH]
        mydict2[DBH] = cur_area
        print mydict2

    else:#This is the first dictionary entry for this species
        #Create new dict entry with sums and count for this species

        #Add a new entry to mydict2 
        cur_area = mydict2[DBH]
        #print mydict2
mykeys = mydict.keys()
#Print the count for each species
for key in mykeys:
    count = int(mydict[key])
    print "The number of trees of species", key, "=", count

mykeys2 = mydict2.keys()
sum = 0
for key in mykeys2:
    sum += mydict2[key]
print sum
average = sum / count
print average
#print "The average DBH for this species", key, "=", average

Gives me syntax error at line

update mydict2

After commenting it out it says it needs file to read, so I can not reproduce the problem.

Next time push the CODE button right before pasting to get the tags right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.