使用Python修改JSON [英] Modifying JSON with Python

查看:87
本文介绍了使用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()是你的朋友.

这篇关于使用Python修改JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆