用python写一段代码,提取指定txt文件中的具体到区域和街道的地点,并用百度API转化为经纬度,再输出为excel文件
时间: 2024-06-10 08:09:16 浏览: 11
以下是用Python实现提取指定txt文件中的具体到区域和街道的地点,并用百度API转化为经纬度,并输出为Excel文件的代码:
```python
import re
import requests
import json
import xlwt
# 读取txt文件,提取地点信息
with open('location.txt', 'r', encoding='utf-8') as f:
content = f.read()
pattern = r'[\u4e00-\u9fa5]+[\u4e00-\u9fa5路街区]+[\u4e00-\u9fa5]+'
locations = re.findall(pattern, content)
# 调用百度API转化为经纬度
ak = 'your_baidu_ak'
url = 'http://api.map.baidu.com/geocoding/v3/?address={}&output=json&ak={}'
result_list = []
for location in locations:
req_url = url.format(location, ak)
response = requests.get(req_url)
result = json.loads(response.text)
if result['status'] == 0:
lat = result['result']['location']['lat']
lng = result['result']['location']['lng']
result_list.append((location, lat, lng))
# 将结果输出为Excel文件
workbook = xlwt.Workbook(encoding='utf-8')
worksheet = workbook.add_sheet('Locations')
for i, result in enumerate(result_list):
worksheet.write(i, 0, result[0])
worksheet.write(i, 1, result[1])
worksheet.write(i, 2, result[2])
workbook.save('locations.xls')
```
希望能够帮助到你,如果你有其他问题,可以直接问我。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)