Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Python修改JSON_Python_Json_Geojson - Fatal编程技术网

用Python修改JSON

用Python修改JSON,python,json,geojson,Python,Json,Geojson,有关此问题的代码,请访问 我编写了一个脚本,它解析csv文件中的地址,使用geopy查询相应的坐标(经度和纬度),并将输出写入JSON格式的文本文件 将数据写入JSON文件的print语句不漂亮: print('{"type": "Feature","geometry": { "coordinates": ['+str(location.longitude)+ ','+str(location.latitude)+ ',],"type": "Point"},"properties": {

有关此问题的代码,请访问

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

将数据写入JSON文件的print语句不漂亮:

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"}},')

必须有更好(更容易)的方法来做到这一点。我开始在谷歌上搜索,但对我的发现并不满意。有人对如何在Python中更优雅地处理JSON有什么建议吗

和您是朋友。

推荐使用
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)

是的,正如谷歌粗略地告诉你的那样,标准库中有一个非常好的
json
模块。为什么要投反对票?所以这不是一个用代码示例发布合法问题的论坛?我访问了json库,但也许我只是想了解更广泛的情况。我没有否决投票,但一般来说,当人们认为问题显示出缺乏研究努力时,往往会这样做。那么最好删除该问题。我要求的是一个更优雅的方法,而不是唯一的方法。可能是如果你指出谷歌为你找到了什么,以及你具体不满意的地方,它可能已经避免了否决票。