我想从excel文件中读取大量的坐标(关于14000+),并通过百度地图的API将它们转换成特定的地址。但是程序只能读取最后的坐标。我的代码有什么问题吗?这是我的代码:
import requests
import pandas as pd
import json
df = pd.read_excel(r'C:\Users\YDC\Desktop\JW.xlsx')
fw = open(r'C:\Users\YDC\Desktop\result.txt', "w", encoding="utf-8")
for i in range(0,len(df)):
t1=df.iloc[i]['lat']
t2=df.iloc[i]['lng']
baiduUrl = "http://api.map.baidu.com/geocoder/v2/?ak=21q0bMSgjdDVe0gLmjClrsuyUA1mvsRx&callback=renderReverse&location=%s,%s&output=json&pois=0" % (t1, t2)
req = requests.get(baiduUrl)
content = req.text
content = content.replace("renderReverse&&renderReverse(", "")
content = content[:-1]
baiduAddr = json.loads(content)
country = baiduAddr["result"]["addressComponent"]["country"]
city = baiduAddr["result"]["addressComponent"]["city"]
province = baiduAddr["result"]["addressComponent"]["province"]
new_line = country + "|" + city + "|" + province
fw.write(new_line)
fw.write("\n")
print(new_line)它只能打印最后一个坐标的地址:捷克共和国\Olomouc\\Olomouc\\Olomouc
如何获得其余的这些坐标?下面是excel文件中的数据
发布于 2019-04-11 03:53:42
这看起来像是一个经典的python循环。
考虑到这一点:
for i in range(0, 10):
foo = i
print (foo) # notice the indentation 输出
9这是因为在python中,变量范围是这样的,您仍然可以从循环外部引用在循环中定义的变量。
这样一个非常简单的修复方法:
for i in range(0, 10):
foo = i
print (foo)给出预期结果
0
1
2
3
4
5
6
7
8
9在您的示例中,只需确保向前第12行向右缩进一个级别即可。
https://stackoverflow.com/questions/55623552
复制相似问题