JSON Modules & Pickle Modules

Source: Internet
Author: User

Before I learned to use the eval built-in method to turn a string into a Python object, the Eval method is limited and can be used for normal data types, json.loads and eval, but when you encounter a special type, eval doesn't work. So the focus of eval is usually to execute a string expression and return the value of the expression.

Import Jsonx="[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:

#Serialization of----------------------------ImportJSON dic={'name':'Alvin',' Age': 23,'Sex':'male'}Print(Type (DIC))#<class ' dict ' >J=json.dumps (DIC)Print(Type (j))#<class ' str ' >F=open ('Serializing Objects','W') F.write (j)#-------------------equivalent to Json.dump (dic,f)f.close ()#-----------------------------deserialization <br>Importjsonf=open ('Serializing Objects') Data=json.loads (F.read ())#equivalent to Data=json.load (f)

Import JSON # dct= "{' 1 ': 111}" #json not recognized single quotes # dct=str ({"1": 111}) #报错 because the generated data is still single quotes: {' One ': 1} DCT='{' 1 ': ' 111 '}'print(json.loads (DCT))# Conclusion: #         No matter how the data is created, as long as the JSON format is satisfied, it can be json.loads out, not necessarily dumps data to loads attention .

Pickle
##----------------------------SerializationImportPickle dic={'name':'Alvin',' Age': 23,'Sex':'male'} Print(Type (DIC))#<class ' dict ' >J=pickle.dumps (DIC)Print(Type (j))#<class ' bytes ' >F=open ('Serialized Object _pickle','WB')#Note is that W is written STR,WB is written bytes,j is ' bytes 'F.write (j)#-------------------equivalent to Pickle.dump (dic,f)f.close ()#-------------------------deserializationImportPicklef=open ('Serialized Object _pickle','RB') Data=pickle.loads (F.read ())#equivalent to 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.

JSON Modules & Pickle Modules

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.