1 导入模块


  • pickle模块适用Python中所有数据类型
    1. import pickle
    2. import time

2 数据类型转二进制文件


  1. structTime = time.localtime(time.time())
  2. with open('structTime.txt', 'wb') as file:
  3. pickle.dump(structTime, file)

3 二进制文件转数据类型


  1. with open('structTime.txt', 'rb') as file:
  2. structTime = pickle.load(file)
  3. print(structTime)

4 数据类型转二进制


  1. string = pickle.dumps(structTime)
  2. print(type(string))

5 二进制转数据类型


  1. structTime = pickle.loads(string)
  2. print(type(structTime))

参考文件


pickle模块.py