首页 > 解决方案 > 如何在反向编码期间将 lat、lon 值从 csv 文件放入 url

问题描述

我有一个与此类似的 URL:https ://nominatim.openstreetmap.org/reverse?format=xml&lat=17.978733&lon=79.14550876&zoom=14&addressdetails= 1 我想更改 csv 文件中的latlon值。

import csv 
import requests 
import xml.etree.ElementTree as ET 

def loadxml(): 

    # url of rss feed 
    url = 'https://nominatim.openstreetmap.org/reverse?format=xml&lat=25.6&lon=78.0176&zoom=14&addressdetails=1'

    # creating HTTP response object from given url 
    resp = requests.get(url) 

    # saving the xml file 
    with open('ldn.xml', 'wb') as f: 
        f.write(resp.content) 

loadxml()

这是一个命令,lat lon但我想要我的 csvlatlon值。

标签: pythonxmlpython-3.x

解决方案


可以按如下方式创建新的 url:

def get_url(lat,lon):
    return 'https://nominatim.openstreetmap.org/reverse?format=xml&lat='+str(lat)+'&lon='+str(lon)'&zoom=14&addressdetails=1

假设您的数据框被称为 df 并且包含纬度和经度的列分别具有名称 lat 和 lon ,如下所示:

for index, row in df.iterrows():

    url = get_url(row[lat],row[lon])

    resp = requests.get(url) 


    with open('ldn.xml', 'wb') as f: 
        f.write(resp.content) 

希望这可以帮到你。


推荐阅读