AttributeError: 'str' object has no attrib

2019-09-01 18:27发布

问题:

I'm writing a code using python to generate a point shapefile within ArcMAP. I have a 1000 random possibilities (in FileA), and I need to try all of them. The FileA is the index of the position of FileB.

In addition, for each sequence generated I'm looking at the evolution of the area of the Voronoi polygon from 3 points to 50 points in each sequence that contains a total of 50 points.

When I run the code, I have this message which confuse me:

tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]))
AttributeError: 'str' object has no attribute 'toInteger'

I'll appreciate any help !

This is my code:

import csv

# create 1000 XY sequences
print 'Start of the script'
sequences = csv.reader(open('FileA.csv','rb'),delimiter=',')
coordinates = []

# read coordinates of volcanos and store in memory
print 'Start reading in the coordinates into the memory'
coordinates_file = csv.reader(open('FileB.csv','rb'),delimiter=',')   
for coordinate in coordinates[1:]:
    coordinates.append(coordinate)
del coordinates_file

##i = 1
for sequence in sequences:
##    print 'sequence # {0}'.format(i) 
    j = 1           
    tempXYFile = csv.writer(open('tempXY.csv','wb'),delimiter=',') #add the parameter to create a file if does not exist                  
    for entry in sequence:         
        tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]))
        print 'Entry # {0}: {1},{2}'.format(j, coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]) 
        j = j + 1
    i = i + 1
    del tempXYFile

print 'End of Script'

回答1:

Python doesn't have a toInteger() function for strings you will want to user int(entry[]) instead.



回答2:

There is no toInteger() method on strings in standard Python. The error message is pretty clear. If you want to convert a string like "37" to the integer 37, try int(entry) instead.

Why did you write toInteger() in your code?



回答3:

Does not exist toInteger method on strings Check this: http://docs.python.org/library/stdtypes.html#string-methods

The correct form is:

coordinates[int(entry)][0]