用 Python 修改 JSON
Modifying JSON with Python
属于这个问题的代码在 github.
上可用
我编写了一个脚本来解析 csv 文件中的地址,使用 geopy 查询相应的坐标(经度和纬度)并将输出写入 JSON 格式的文本文件。
将数据写入JSON文件的打印语句不漂亮:
print('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')
time.sleep(0.01)
file.write('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')
一定有更好(更简单)的方法来做到这一点。我已经开始 google 了,但对我的发现并不满意。有人对如何更优雅地处理 Python 中的 JSON 有什么建议吗?
json.dumps() and json.loads() 是你的朋友。
json
模块会很好地为您服务,推荐使用。以下内容也适用:
output = '{{"type": "Feature","geometry": {{ "coordinates": [{},{},],"type": "Point"}},"properties": {{"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}}}},'.format(location.longitude, location.latitude))
print(output)
time.sleep(0.01)
file.write(output)
属于这个问题的代码在 github.
上可用我编写了一个脚本来解析 csv 文件中的地址,使用 geopy 查询相应的坐标(经度和纬度)并将输出写入 JSON 格式的文本文件。
将数据写入JSON文件的打印语句不漂亮:
print('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')
time.sleep(0.01)
file.write('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+
','+str(location.latitude)+ ',],"type": "Point"},"properties": {"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}},')
一定有更好(更简单)的方法来做到这一点。我已经开始 google 了,但对我的发现并不满意。有人对如何更优雅地处理 Python 中的 JSON 有什么建议吗?
json.dumps() and json.loads() 是你的朋友。
json
模块会很好地为您服务,推荐使用。以下内容也适用:
output = '{{"type": "Feature","geometry": {{ "coordinates": [{},{},],"type": "Point"}},"properties": {{"title": "dentist #1","privat": true,"marker-color": "#6699ff","marker-size": "large","marker-symbol": "dentist"}}}},'.format(location.longitude, location.latitude))
print(output)
time.sleep(0.01)
file.write(output)