Python中讀寫json文件

在處理標註數據文件的時候,需要用python來讀寫json文件。常用的讀寫方式如下:

# read the local json file
with open('dict.json', 'r') as obj:
    dict = json.load(obj)

# write the dict to local json file
with open('test.json', 'w') as obj:
    json.dump(dict, obj, indent=2)

在寫入json文件的時候,需要加上參數"indent=2"以避免所有數據輸出到同一行。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章