Reputation: 11
I am trying to find a quicker way to find the closest match of Latitude & Longitude in a list of Lat/Longs from a massive file where I have to open a csv file, convert it to the correct format. Choose the columns my lat and long are located and then plug them into my eqation...if it works. This is what I have so far...I keep getting
TypeError:
float
object is not subscriptable
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)
Upvotes: 1
Views: 963
Reputation: 178021
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.
Upvotes: 1