使用Python修改JSON

问题描述:

属于此问题的代码可在 github 中找到.

The code belonging to this question is available on github.

我编写了一个脚本,该脚本解析来自csv文件的地址,使用geopy查询相应的坐标(经度和纬度),并将输出以JSON格式写入文本文件.

I've written a script that parses addresses from a csv file, queries the corresponding coordinates (longitude and latitude) using geopy and writes the output to a text file in the JSON format.

将数据写入JSON文件的打印语句并不美观:

The print statement that writes the data into a JSON file is not beautiful:

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?

There must be a better (easier) way to do this. I've started to google around, but am not satisfied with what I'm finding. Does anybody have suggestions on how to handle JSON in Python more gracefully?

json.dumps() json.loads()是你的朋友.