In my personal experience, I suggest using JSON if:
- You want a human-readable file,
- A file type that is compatible across multiple technologies without very much implementation overhead.
- An easily editable file (as it can simply be a text file).
I would suggest using Pickle if:
- The majority of the project will be written in python.
- The dictionary is likely to become very large/complex or rolled into a larger class.
- A non-human readable file is required.
- A file that is difficult to edit without prior knowledge of its construction.
Based on the situation you touch upon in your question, JSON would be the more beneficial choice.
# example code here
import json
d = {1: 'a', 2: 'b', 3: 'c'}
with open('C:\temp.txt', 'w') as file:
json.dump(d, file)
with open('C:\temp.txt', 'r') as file:
new_d = json.load(file)
>>> new_d
{u'1': u'a', u'3': u'c', u'2': u'b'}
Cite from:
https://stackoverflow.com/questions/46525235/save-dict-to-pickle-or-write-to-file
theory原理
数据的Serialize and Deserialize 序列化和反序列化
My solution (with pickle)
# python built-in package
# don't need to install
import pickle
# Use mode='wb',Write and Serialize into Binary
with open(file='./Data/data.pkl', mode='wb') as f:
pickle.dump(obj=data, file=f)
# Use mode='rb',Read and Deserial from Binary and read
with open(file='./Data/data.pkl', mode='rb') as f:
data_restore = pickle.load(f)