JSON转储格式Python

Lea*_*wly -1 python json

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

我读取的JSON文件links.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"}
]
Run Code Online (Sandbox Code Playgroud)

我打开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)
Run Code Online (Sandbox Code Playgroud)

这样成功导出,我可以使用文本编辑器(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"}]
Run Code Online (Sandbox Code Playgroud)

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

Ish*_*ael 8

有一个名为的参数indent。这是None在默认情况下,它的意思是“没有漂亮打印”。如果将其设置为整数值,它将启用漂亮打印,并使用那么多空格缩进嵌套的元素。

在您的情况下,它将类似于以下内容:

json.dump(links, outfile, indent=4)
Run Code Online (Sandbox Code Playgroud)

(或者indent=2如果您希望减少空间)。这是docs(link)中的一个示例,该示例显示了您在进行过程中可能还需要的更多功能:

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


ig0*_*774 5

indent参数允许一定程度的漂亮打印。

从文档:

如果缩进是一个非负整数,那么 JSON 数组元素和对象成员将使用该缩进级别进行漂亮打印。缩进级别 0 或负数只会插入换行符。无(默认)选择最紧凑的表示。