JSON and Pickle

Source: Internet
Author: User

JSON and Pickle

Two modules for serialization

JSON, used to convert between string and Python data types
Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load

JSON dumps convert data types to strings

Dump converts the data type into a string and stores it in a file

Loads converting a string to a data type

Load converts a file from a string to a data type

You can see that the direct operation of the file is not with "s" Dump and load, directly to the memory operation is the "s".

Pickle


Now there is a scene in the exchange of data between different devices very low way is to transfer files, dumps can directly send the memory of server A to other servers, such as B server, in many scenarios if you use pickle, that A and B programs are Python program This is not realistic , a lot of the time is different between the memory exchange between the program what to do? Using JSON and JSON can dump the results more readable, then someone asked, that still use pickle do not directly useJSON, so JSON can only serialize common data types (lists, dictionaries, lists, strings, numbers), like date formats, class object JSON, and only use pickle. Why can't he serialize the above stuff? Because JSON is cross-lingual!


Let's look at a small example of JSON:

  
 
  1. import requests
  2. import json
  3. response = requests.get(‘http://wthrcdn.etouch.cn/weather_mini?city=北京‘)
  4. response.encoding = ‘utf-8‘
  5. dic = json.loads(response.text)
  6. print(type(dic))
  7. print(dic)
  8. print(dic[‘data‘][‘city‘])
Output: This is the processing of data in memory, we are looking at the processing of a data in a file: The storage format of the data within the file:
  
 
  1. [
  2. { "title" : [ ", "reply" Span class= "pun" >: [ "4" "author" : [ "zzy64421"
  3. { "title" : [ ", "reply" : [ "1" "author" : [ "\U6709\U70B9\U5C0F\U538C\U4E16" Span class= "pun")},
  4. { "title" : [ " , "reply" : [ "7" "author" : Span class= "pun" >[ "\u94c1\u8840\u591c\u5e1d" ]},
  5. { "title" : [ ", "reply" : [ "4" ], Span class= "PLN" > "author" : Span class= "pun" >[ "C\u8c6ay" ]},
  6. { "title" : [ ", "reply" : Span class= "pun" >[ "$" ], "author" : [ "\u5c0f\u50bb\u86cb\u5a03\u5a03" ]},
  7. { "title" : [ ", "reply" : [ "0" ], Span class= "PLN" > "author" : Span class= "pun" >[ "\u5de6\u4ed3\u871c\u67d1" ]},
  8. { "title" : [ ", "reply" Span class= "pun" >: [ "Wuyi" "author" : [ "fhg1225"
  9. { "title" : [ ", "reply" : Span class= "pun" >[ "9" ], "author" : [ "200901491" ]},
  10. {"title": ["\u6709\u722c\u866b\u8f6f\u4ef6\u53ef\u4ee5\u91c7\u96c6\u5fae\u535a\u3001\u8d34\u5427\u3001\u77e5\u4e4e\u7684\u4e48\uff1f"], "reply": ["3"], "author": ["\u738b\u9053\u653b\u7565"]}
  11. ]
Overall, this is a list, and each element in the list is a dictionary.
  
 
  1. import json
  2. f = open(‘items.json‘)
  3. a = json.load(f)
  4. print(type(a))
  5. print(a[0])
  6. print(a[0][‘author‘])
  7. f.close()
Output Result:
With these two examples we see the use of JSON and the difference between load and loads.

Note: Double quotes must be used inside the string, such as"Reply": ["3"], cannot be written as: 'reply ': ['3 ']. because JSON is cross-lingual, other languages use double quotes to denote strings, so be aware of this in Python


Null

JSON and Pickle

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.