2019年3月7日 星期四

[python] 最簡單的方式 read/write json file

最簡單的方式 read/write json file

井民全, Jing, mqjing@gmail.com

Read
---------------------------------------------------
import json
from io import StringIO
import numpy as np

with open('myfile.json', 'r') as read_file:
    dict_data = json.load(read_file)
   
    # list all keys
    print('dict_data.keys = ', dict_data.keys())

---------------------------------------------------




Write

---------------------------------------------------
import json

data = {'key1': value1, 'key2': value2}
ret = json.dumps(data)

open('out.json', 'w') with fp:
    fp.write(ret)
---------------------------------------------------


Further Reading