Python 序列化数据

嘉美伯爵

数据处理 JSON 187

注意

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

Python load dump

  • loads/dumps
import json

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

sdt = '{"name": "Gage", "age": 25}'
print(json.loads(sdt))
  • load/dump
with open(r'C:\Users\Gage\Desktop\新建文件夹 (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
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
pdt = pickle.dumps(dt)
print(pdt)

print(pickle.loads(pdt))

pandas dump/load

with open(r'C:\Users\Gage\Desktop\新建文件夹 (2)\qinghai-gejie-20200221.txt', 'r', encoding='utf-8') as f:
    print(pd.read_json(f))