gpt4 book ai didi

python - JSON 转储格式 Python

转载 作者:行者123 更新时间:2023-12-02 02:34:36 24 4
gpt4 key购买 nike

我正在读取一个 JSON 文件,添加一个字段,然后写入一个新的 JSON 文件。

我在 links.json 中读取的 JSON 文件如下所示:

[{"negativeNode":"osgb4000000023183407","toid":"osgb4000000023296573","term":"Private Road - Restricted Access","polyline":[492019.481,156567.076,492028,156567,492041.667,156570.536,492063.65,156578.067,492126.5,156602],"positiveNode":"osgb4000000023183409","index":1,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023763485","toid":"osgb4000000023296574","term":"Private Road - Restricted Access","polyline":[492144.493,156762.059,492149.35,156750,492195.75,156630],"positiveNode":"osgb4000000023183408","index":2,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023183650","toid":"osgb4000000023296638","term":"Private Road - Restricted Access","polyline":[492835.25,156873.5,493000,156923,493018.061,156927.938],"positiveNode":"osgb4000000023183652","index":3,"nature":"Single Carriageway"}
,{"negativeNode":"osgb4000000023181163","toid":"osgb4000000023388466","term":"Local Street","polyline":[498136.506,149148.313,498123.784,149143.969,498119.223,149143.411,498116.43,149143.318,498113.638,149145.179],"positiveNode":"osgb4000000023806248","index":4,"nature":"Single Carriageway"}
]

我打开 JSON 文件,读取它,创建一个新字段,然后将其转储到一个新文件中:

import json
links_file = open('links.json')
links = json.load(links_file)

for link in links:
link['length'] = 10

with open('links_new.json','w') as outfile:
json.dump(links, outfile)

这成功导出,我可以使用文本编辑器进行检查( Sublime Text ):

[{"index": 1, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023183407", "toid": "osgb4000000023296573", "length": 10, "polyline": [492019.481, 156567.076, 492028, 156567, 492041.667, 156570.536, 492063.65, 156578.067, 492126.5, 156602], "positiveNode": "osgb4000000023183409"}, {"index": 2, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023763485", "toid": "osgb4000000023296574", "length": 10, "polyline": [492144.493, 156762.059, 492149.35, 156750, 492195.75, 156630], "positiveNode": "osgb4000000023183408"}, {"index": 3, "term": "Private Road - Restricted Access", "nature": "Single Carriageway", "negativeNode": "osgb4000000023183650", "toid": "osgb4000000023296638", "length": 10, "polyline": [492835.25, 156873.5, 493000, 156923, 493018.061, 156927.938], "positiveNode": "osgb4000000023183652"}, {"index": 4, "term": "Local Street", "nature": "Single Carriageway", "negativeNode": "osgb4000000023181163", "toid": "osgb4000000023388466", "length": 10, "polyline": [498136.506, 149148.313, 498123.784, 149143.969, 498119.223, 149143.411, 498116.43, 149143.318, 498113.638, 149145.179], "positiveNode": "osgb4000000023806248"}]

如您所见,这不像原始 JSON 文件那样具有视觉可读性。我能够成功地逐行读取它,但它在 Sublime Text 中打印为一行。我缺少 JSON 转储的格式化方面吗?

最佳答案

有一个名为indent的参数。默认情况下它是None,这意味着“没有 pretty-print ”。如果将其设置为整数值,它将启用 pretty-print ,并使用那么多空格来缩进嵌套元素。

就您而言,它将类似于:

json.dumps(links, outfile, indent=4)

(或者indent=2,如果您喜欢更少的空格)。以下是文档 ( link ) 中的示例,其中显示了您在学习过程中可能还需要的更多功能:

>>> import json
>>> print json.dumps({'4': 5, '6': 7}, sort_keys=True,
... indent=4, separators=(',', ': '))
{
"4": 5,
"6": 7
}

关于python - JSON 转储格式 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com