当前位置:   article > 正文

python--对象序列化和反序列化以及with语句块的使用_python序列化和反序列化方法

python序列化和反序列化方法

1.对象序列化和反序列化

对象序列化:将对象这种抽象概念转化为可以传输存储的物理概念

对象反序列化:将磁盘或者网络间的物理数据转化为对应的编程语言的对象

对象持久化:将对象永久的存储下来

对相反持久化: 将磁盘上永久存储的数据读取内存中

pickle:

                将对象序列化为字节数据

                        dump()        ----对象序列化

                        dumps()

                        load()

                        loads()        ----对象反序列化

json:

                将对象序列化转化为字符数据

                        dump()        ----对象序列化

                        dumps()

                        load()

                        loads()        ----对象反序列化 

2.pickle-- dumps():

                将对象序列化为字节数据

                        dump()        ----对象序列化

                        dumps()

 

  1. >>> users
  2. ['qf', 'dy', 'xz', 'zjl']
  3. >>> import pickle
  4. >>> pickle.dumps(users)
  5. b'\x80\x04\x95\x1a\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x02qf\x94\x8c\x02dy\x94\x8c\x02xz\x94\x8c\x03zjl\x94e.'
  6. >>> comtent = pickle.dumps(users)
  7. >>> comtent
  8. b'\x80\x04\x95\x1a\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x02qf\x94\x8c\x02dy\x94\x8c\x02xz\x94\x8c\x03zjl\x94e.'
  9. >>> f = open("d:\\a.txt", "bw")
  10. >>> f.write(comtent)
  11. 37
  12. >>> f.flush()

 在d盘中创建a.txt

 

3.pickle-- loads():

                将对象序列化为字节数据

                        load()

                        loads()        ----对象反序列化

 

  1. >>> help(pickle.loads)
  2. Help on built-in function loads in module _pickle:
  3. loads(data, /, *, fix_imports=True, encoding='ASCII', errors='strict', buffers=())
  4. Read and return an object from the given pickle data.
  5. The protocol version of the pickle is detected automatically, so no
  6. protocol argument is needed. Bytes past the pickled object's
  7. representation are ignored.
  8. Optional keyword arguments are *fix_imports*, *encoding* and *errors*,
  9. which are used to control compatibility support for pickle stream
  10. generated by Python 2. If *fix_imports* is True, pickle will try to
  11. map the old Python 2 names to the new names used in Python 3. The
  12. *encoding* and *errors* tell pickle how to decode 8-bit string
  13. instances pickled by Python 2; these default to 'ASCII' and 'strict',
  14. respectively. The *encoding* can be 'bytes' to read these 8-bit
  15. string instances as bytes objects.
  16. >>> f = open("d:\\a.txt", "br")
  17. >>> res = f.read()
  18. >>> res
  19. b'\x80\x04\x95\x1a\x00\x00\x00\x00\x00\x00\x00]\x94(\x8c\x02qf\x94\x8c\x02dy\x94\x8c\x02xz\x94\x8c\x03zjl\x94e.'
  20. >>> pickle.loads(res)
  21. ['qf', 'dy', 'xz', 'zjl']

4.pickle--dump load

  1. >>> import pickle
  2. >>> pickle.dump(users, open("d:\\b.txt","bw"))
  3. >>>
  4. >>>
  5. >>> pickle.load(open("d:\\b.txt","br"))
  6. ['qf', 'dy', 'xz', 'zjl']
  7. >>>

 

5.json--dump,load 

  1. >>> import json
  2. >>> json.dump(users, open("d:\\a.txt", "w"))
  3. >>>
  4. >>>
  5. >>> json.load(open("d:\\a.txt", "r"))
  6. ['qf', 'dy', 'xz', 'zjl']
  7. >>>

 6.with模块---防止忘记关闭IO流

  1. with open("a.txt.txt", "r", encoding="utf-8") as f: #f = open()
  2. content = f.read()
  3. print(content)
  1. i love China
  2. Process finished with exit code 0

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/405046?site
推荐阅读
相关标签
  

闽ICP备14008679号