Python-將JSON資料寫入檔案

將JSON資料寫入檔案

 

import json
with open('test.json', 'w', encoding='utf-8') as f:
     json.dump([{'id': '1', 'city': '台北'}, {'id': '2', 'city': '高雄'}], f, indent=10, sort_keys=True, ensure_ascii=False)
 #sort_keys=True #是否排序Key 
 #indent=4 #縮排
 #ensure_ascii=False  #編碼

test.json

[{'id': '1', 'city': '台北'}, {'id': '2', 'city': '高雄'}]

 

Yiru@Studio - 關於我 - 意如