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
  1. # write to file
  2. import json
  3. file_path='./dic1.json'
  4. dic1 = {'list1':[1,2,3,4]}
  5. dic1
  6. with open(file_path, 'w') as f:
  7. json.dump(dic1, f)
  8. # read from file
  9. import json
  10. file_path='./dic1.json'
  11. with open(file_path) as f:
  12. dic2 = json.load(f)
  13. list2=dic2['list1']
  14. list2
執行結果:
``` [1, 2, 3, 4] ```

沒有留言:

張貼留言