json模块和pickle模块
import json
s = ['sadf','123']
ss = json.dumps(s) #仅转换为json标准字符串
print(type(ss),ss)
sss = json.loads(ss) #反序列化
print(sss,type(sss))
<class 'str'> ["sadf", "123"]
['sadf', '123'] <class 'list'>
f = open(r'123\test.json','w')
json.dump(s,f) #dump 文件处理时,转换字符串后写入文件
f = open(r'123\test.json','r')
data = json.load(f) #load 读取文件并提取数据
print(data[1])
123
json:支持str,int,tuple,list,dict
import pickle
with open(r'123\test.pkl','wb') as pk:
pickle.dump(s,pk)
pk = pickle.dumps(s)
print(pk,type(pk))
b'\x80\x03]q\x00(X\x04\x00\x00\x00sadfq\x01X\x03\x00\x00\x00123q\x02e.' <class 'bytes'>
with open(r'123\test.pkl','rb') as pk:
data = pickle.load(pk)
print(data)
['sadf', '123']
支持所有数据类型,但仅限于python中,不能跨平台