Python 序列化数据

2021-01-18 10:57:39 浏览数 (1)

注意

两点说明:处理数据用pandas,生成数据用numpy,在处理json数据的时候由于数据连贯,我们不能按行读取,因此需要 使用pandas直接读取,可以加快读取速度

Python load dump

  • loads/dumps
代码语言:javascript复制
import json

dt = {'name': 'Gage', 'age': 25}
print(json.dumps(dt))

sdt = '{"name": "Gage", "age": 25}'
print(json.loads(sdt))
  • load/dump
代码语言:javascript复制
with open(r'C:UsersGageDesktop新建文件夹 (2)qinghai-gejie-20200221.txt', 'r', encoding='utf-8') as f:
    print(json.load(f))

with open('new.txt', 'w') as f:
    json.dump(dt, f)

pickle dump/load

pickle 可以保密一些数据,但被破环将不能读取

  • dump/load
代码语言:javascript复制
import pickle

dt = {'name': 'Gage', 'age': 25}
with open('new.txt', 'wb') as f:
    pickle.dump(dt, f)

with open('new.txt', 'rb') as f:
    print(pickle.load(f))
  • dumps/loads
代码语言:javascript复制
pdt = pickle.dumps(dt)
print(pdt)

print(pickle.loads(pdt))
pandas dump/load
代码语言:javascript复制
with open(r'C:UsersGageDesktop新建文件夹 (2)qinghai-gejie-20200221.txt', 'r', encoding='utf-8') as f:
    print(pd.read_json(f))

0 人点赞