AttributeError: 'float' - Objekt hat kein Attribut 'append' - Python-Dictionary

Ich bin versucht, erstellen Sie ein neues Wörterbuch, das eine Liste der Arten von Holz, so wie auch die von der DBH für diese Arten. Es wird mehrere DBH ist für jede Spezies. Es zieht diese information aus einer text-Datei.

Den Teil, der schafft das erste Wörterbuch arbeiten (Listen Sie die Arten und die Anzahl für jedes), aber ich kann es nicht Anhängen, die DBH ist für jede Spezies. Ich weiterhin die Fehlermeldung AttributeError: 'float' - Objekt hat kein Attribut 'append'. Ich habe gesucht und gesucht und versucht, mehrere Möglichkeiten, aber kann nicht ankommen dieses zu wirken.

import string, os.path, os, sys


filepath = "C:\\temp\\rdu_forest1.txt"
data=[]
#Open the text file
myfile=open(filepath,'r')
#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
    data.append([float(myline[0]),float(myline[1]),myline[2].rstrip(),float(myline[3].rstrip())]) 
    #rstrip removes white space from the right side
    count = count + 1
    row = myfile.readline()
myfile.close()
mydict={}

mydict2={} #Create an emyty mydict2 here  *********

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

        #update mydict2 here  *********
        mydict2[species].append(DBH)

    else:#This is the first dictionary entry for this species
        #Create new dict entry with sums and count for this species
        mydict[species]=1
        mydict2[species]=DBH #Add a new entry to mydict2 here  *********

print mydict

Ist hier der TraceBack

Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec codeObject in __main__.__dict__
  File "E:\Python\16\dictionary.py", line 40, in <module>
    mydict2[species].append(DBH)
AttributeError: 'float' object has no attribute 'append'
  • Bitte geben Sie die traceback. Das zeigt die genaue Zeile, in der die Ausnahme ausgelöst wird. Es wird offensichtlich sein, eine der Linien, die mit .append(). Als der Fehler msg sagt, Sie entdecken, dass Sie versuchen zum Anhängen an einem Schwimmer.
  • Traceback (most recent call last): File "C:\Python27\ArcGIS10.1\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py" Linie 326, in RunScript exec codeObject in main.__dict__ File "E:\Python\16\dictionary.py", line 40, in <module> mydict2[Arten].append(DBH) AttributeError: 'float' - Objekt hat kein Attribut 'append'
  • So mydict2[species] ist ein float. Erwarten Sie, dass es ein float? Sie können nicht anfügen an ein float.
  • Ist das komisch? Ich bekomme keine Fehler.
  • Dies ist der Wert der mydict: {'LOB': 95, 'SEIN': 1, 'WD': 10, 'WO': 95, 'HK': 19, 'YP': 33, 'POP': 12, 'RB': 3, 'RM': 71, 'ASCHE': 2, 'LP': 696, 'SLP': 1, 'VP': 1, 'SRW': 2, 'SHL': 17, 'CV': 1, 'RO': 82, 'MPL': 13, 'SP': 1, 'SW': 11, 'MW': 1, 'SL': 21, 'SG': 82} ich will mydict2 werden: {'LOB': [102, 14, 203], 'SEIN': [212, 232]...} Die erste zeigt die Arten und die Anzahl der Bäume mit, dass sich die Arten. Der zweite Teil zeigt die Art und den Durchmesser des Baums für diese Arten. Vielleicht gibt es einen besseren Weg, um diese Werte dort. Ich bin Total neu auf diesem Konzept.
InformationsquelleAutor user2923636 | 2013-10-26
Schreibe einen Kommentar