2017年7月11日 星期二

Python - How to write a list to a file and read it as a list type using json - list與file的讀寫IO

Python version :Python 3.6.0 :: Anaconda 4.3.1 (64-bit)
Pandas version :0.19.2
System version :Windows 10
# write to file
import json
file_path='./dic1.json'
dic1 = {'list1':[1,2,3,4]}
dic1
with open(file_path, 'w') as f:  
    json.dump(dic1, f)

# read from file
import json
file_path='./dic1.json'
with open(file_path) as f:
    dic2 = json.load(f)
list2=dic2['list1']
list2
執行結果:
``` [1, 2, 3, 4] ```

沒有留言:

張貼留言