1

我试图找到一种更快的方法来从一个大文件中找到纬度和经度最接近的匹配项,我必须在该文件中打开一个 csv 文件,将其转换为正确的格式。选择我的 lat 和 long 所在的列,然后将它们插入我的 eqation ......如果它有效。这就是我到目前为止所拥有的......我不断得到

TypeError: float对象不可下标

def menu3(data):
    lat2 = 47.488611
    long2 = -117.578611
    data = open('eqdata.csv', 'r')
    reader = data.readlines()
    for line in reader:
        lat1 = line[4]
    for line in reader:
        long1 = line[5]
    for ix in range(len(long1)):
        lat1[ix][5]=int(long2[ix][5])

    for ix in range(len(lat1)):
        lat1[ix][4]=int(lat1[ix][4])

    distance =int((lat2-lat1)**2)**.5+int((long2-long1)**2)
    distanceSq = distance**2
    print (distanceSq)
4

1 回答 1

1

reader = data.readlines() reads all the lines of the file into a a list of strings.

These lines:

for line in reader:
    lat1 = line[4]

assign lat1 to the 5th character of each line...but it loops until the last line, resulting in lat1 being the 5th character of the last line in the file...probably not what you want.

This doesn't even seem to be the code that gives you that error message, because given an input file this line:

    lat1[ix][5]=int(long2[ix][5])

Since lat1 is a single character, should give:

IndexError: string index out of range

long2 is a float value assigned earlier, so indexing it would fail as well.

Update your question with the actual source, sample input and a full traceback of the error message for better help.

There is also a csv module that helps process .csv files, but still need some sample input.

Also I suggest using a debugger, or at least some print statements, to see what your variables are doing.

于 2013-06-09T04:47:01.343 回答