Python JSON & Pickle & Shelve Module

Source: Internet
Author: User
Tags serialization

JSON & Pickle Before we learned to use the eval built-in method to turn a string into a Python object, the Eval method has limitations, for normal data types, Both Json.loads and Eval are available, but when a particular type is encountered, eval is not used, so the focus of eval is usually used to execute a string expression and return the value of the expression.
1234 importjsonx="[null,true,false,1]"print(eval(x))print(json.loads(x))
What is serialization?

The process of changing an object (variable) from memory to a storage or transfer is called serialization, and in Python it is called pickling, which is also called serialization,marshalling,flattening in other languages, and so on.

After serialization, the serialized content can be written to disk or transferred over the network to another machine.

In turn, re-reading the variable contents from the serialized object into memory is called deserialization, i.e. unpickling.

Json

If we are going to pass objects between different programming languages, we have to serialize the object into a standard format, such as XML, but the better way is to serialize it to JSON, because JSON represents a string that can be read by all languages, easily stored to disk, or transmitted over a network. JSON is not only a standard format, but also faster than XML, and can be read directly in the Web page, very convenient.

JSON represents objects that are standard JavaScript language objects, and JSON and Python have built-in data types that correspond to the following:

1234567891011121314151617 #----------------------------序列化importjsondic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘}print(type(dic))#<class ‘dict‘>j=json.dumps(dic)print(type(j))#<class ‘str‘>f=open(‘序列化对象‘,‘w‘)f.write(j)  #-------------------等价于json.dump(dic,f)f.close()#-----------------------------反序列化<br>importjsonf=open(‘序列化对象‘)data=json.loads(f.read())#  等价于data=json.load(f)
watch out.Pickle
1234567891011121314151617181920212223 ##----------------------------序列化importpickledic={‘name‘:‘alvin‘,‘age‘:23,‘sex‘:‘male‘} print(type(dic))#<class ‘dict‘>j=pickle.dumps(dic)print(type(j))#<class ‘bytes‘>f=open(‘序列化对象_pickle‘,‘wb‘)#注意是w是写入str,wb是写入bytes,j是‘bytes‘f.write(j)  #-------------------等价于pickle.dump(dic,f)f.close()#-------------------------反序列化importpicklef=open(‘序列化对象_pickle‘,‘rb‘)data=pickle.loads(f.read())#  等价于data=pickle.load(f)print(data[‘age‘])    

The problem with Pickle is the same as for all other programming language-specific serialization problems, that is, it can only be used in Python, and may be incompatible with each other in Python, so it's okay to save only those unimportant data with pickle and not successfully deserialize it.

Shelve module

The shelve module is simpler than the Pickle module, with only one open function, which returns a dictionary-like object, readable and writable; key must be a string, and the value can be a data type supported by Python

123456789101112 importshelve=shelve.open(r‘shelve.txt‘)# f[‘stu1_info‘]={‘name‘:‘alex‘,‘age‘:‘18‘}# f[‘stu2_info‘]={‘name‘:‘alvin‘,‘age‘:‘20‘}# f[‘school_info‘]={‘website‘:‘oldboyedu.com‘,‘city‘:‘beijing‘}### f.close()print(f.get(‘stu_info‘)[‘age‘])

Python JSON & Pickle & Shelve Module

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.