簡體   English   中英

JSON 轉儲格式 Python

[英]JSON dumps format Python

我正在讀取一個 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 ,這意味着“沒有漂亮的打印”。 如果將其設置為整數值,它將啟用漂亮打印,並使用那么多空格來縮進嵌套元素。

在您的情況下,它將是以下內容:

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

(或者indent=2如果你喜歡更少的空格)。 這是文檔(鏈接)中的一個示例,它顯示了您在進行過程中可能還需要的更多功能:

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

indent參數允許一定程度的漂亮打印。

從文檔:

如果縮進是一個非負整數,那么 JSON 數組元素和對象成員將使用該縮進級別進行漂亮打印。 縮進級別為 0 或負值,只會插入換行符。 無(默認)選擇最緊湊的表示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM