我正在尝试编写一个简单的程序,该程序可以通过熊猫导入CSV文件,并使用this模块进行一些地理位置定位,然后将结果写入新的CSV文件中。
我正在使用的模块返回了一个Dict,我应该能够将其拆分为不同的列,但是我似乎找不到任何做到这一点的方法。
我正在使用的数据样本:
ReportTime,ConvertedLat,ConvertedLong
2018-05-21 19:01:39.0000000,33.921127,-118.128477
2018-05-21 19:02:07.0000000,33.921125,-118.128454
2018-05-21 19:07:05.0000000,33.921274,-118.128355
2018-05-21 19:12:58.0000000,33.921248,-118.12841
2018-05-21 19:16:14.7281966,33.921248,-118.12841
2018-05-21 19:17:24.7289005,33.921248,-118.12841
2018-05-21 19:19:33.0000000,33.921268,-118.128418
2018-05-21 19:45:03.0000000,33.919818,-118.128749
2018-05-21 19:46:23.0000000,33.919817,-118.128748
2018-05-21 19:47:23.0000000,33.919754,-118.12907
2018-05-21 19:48:43.0000000,33.919703,-118.129382
2018-05-21 19:50:03.0000000,33.919585,-118.129189
2018-05-21 19:50:37.0000000,33.919383,-118.130163
2018-05-21 20:04:36.0000000,33.919789,-118.129882
2018-05-21 20:23:27.0000000,33.920036,-118.129822
2018-05-21 20:46:12.0000000,33.91993,-118.129499
2018-05-21 20:50:09.0062205,33.91993,-118.129499
2018-05-21 21:01:43.0000000,33.921195,-118.128403
2018-05-21 21:01:48.5683710,33.921195,-118.128403
2018-05-21 21:07:18.0000000,33.921135,-118.128448
2018-05-21 21:22:56.0000000,33.921089,-118.12849
2018-05-21 21:34:18.0000000,33.91897,-118.126026
2018-05-21 21:34:30.7203730,33.91897,-118.126026
2018-05-21 21:34:35.3922356,33.91897,-118.126026
2018-05-21 21:34:37.5172694,33.91897,-118.126026
2018-05-21 21:34:38.6891440,33.91897,-118.126026
2018-05-21 21:35:00.0000000,33.918843,-118.126122
2018-05-21 21:35:40.0000000,33.918683,-118.125967
2018-05-21 21:36:20.0000000,33.918748,-118.126087
2018-05-21 21:37:00.0000000,33.918762,-118.126094
2018-05-21 21:37:39.0000000,33.918776,-118.126102
2018-05-21 21:38:20.0000000,33.918772,-118.126098
2018-05-21 21:40:23.0000000,33.918516,-118.125526
2018-05-21 21:59:09.0000000,33.92636,-118.128706
我编写的代码:
import pandas as pd
import reverse_geocoder as rg
import csv
source = pd.read_csv("Lookup.csv") # source file location
currentRow=0 # counts current row location
rowCount = source.shape[0] # queries number of rows
print(rowCount, "Rows total to be coded")
while currentRow < rowCount:
currentLat = source.iloc[currentRow, 1] # stores the current working latitude
currentLong = source.iloc[currentRow, 2] # stores the current working longitude
currentReportedTime = source.iloc[currentRow, 0] # stores the current reported time
Lat = currentLat
Long = currentLong
coordinates = (Lat, Long)
results = rg.search(coordinates,mode=1) # default mode = 2
print(results)
with open("results.csv", "a", newline='\n') as csv_file:
csv_app = csv.writer(csv_file)
csv_app.writerow([results, currentReportedTime])
currentRow = currentRow + 1
else:
print("Complete")
非常感谢您的协助!
答案 0 :(得分:0)
我不确定您为什么要使用panda读取csv文件和csv进行写入。我的解决方案是读取csv文件并将数据放在列表中,然后调用rg.search(coordinates,mode = 1)模块并再次写出csv。请参阅下面的代码段。当然,这也取决于rg.search(coordinates,mode = 1)的输出格式。我假设结果是一个列表或具有2个元素的元组。
import csv
line_data = []
result = [0, 0]
with open('input.csv') as csvfile:
csvdata = csv.reader(csvfile, dialect='excel', delimiter=',')
for row in csvdata:
row[1] = float(row[1])
row[2] = float(row[2])
line_data.append(row)
for line in line_data:
coordinates = (line[1], line[2])
# now call your rg.search(coordinates, mode=1)
# as I do not have this module I just make result the
# same as line
result[0], result[1] = line[1], line[2]
print('date is {}, Lat is {}, Long is {}'.
format(line[0], result[0], result[1]))
with open('output.csv', 'a') as csvfile:
csvdata = csv.writer(csvfile, delimiter=',')
csvdata.writerow([line[0], result[0], result[1]])