Python JSON & Pickle Module

Source: Internet
Author: User

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

"""
Python 3 JSON module uses
"""
Import Jsondata = {"    Beijing": {"        Chaoyang": ["Three Li Tun", "Wangjing", "Guomao"],        "Haidian": ["Five Crossings", "College Road", "after Factory village"],        "Dongcheng District": ["Dongzhimen", "chongwenmen "," Wangfujing "],    },    " Shanghai City ": {        " Jingan ": [],        " Huangpu ": [],        " Hongkou ": [],    }}

1, Json.dumps dict-->strting

s_dumps = json.dumps (data, Sort_keys=true, indent=4, ensure_ascii=false) print (Type (s_dumps)) print (s_dumps) Output:<' str '>{"    Shanghai City": {        "Hongkou": [],        "Jingan": [],        "Huangpu": []    },    "Beijing": {        "Dongcheng": [            "Dongzhimen",            "Chongwenmen",            "Wangfujing"        ],        "Chaoyang": [            " Sanlitun, "            wangjing",            "Guomao", "        Haidian": [            "Five Crossings",            "College Road",            "rear Factory village"        ]    }}

2, Json.loads string-->dict

r_loads = json.loads (s_dumps) print (Type (r_loads)) print (r_loads) output: <   ' dict '>{' Shanghai ': {' Hongkou ': [], ' Jingan ': [], ' Huangpu ': []}, ' Beijing ': {' Dongcheng District ': [' dongzhimen ', ' chongwenmen ', ' wangfujing '], ' Chaoyang District ': [' Three Li Tun ', ' wangjing ', ' Guomao '], ' Haidian ': [' Five crossing ', ' College Road ', ' Rear Factory village '}}

3. json.dump Write file

With open ("Data.json", "W", encoding= "UTF-8") as F_dump:    s_dump = json.dump (data, F_dump, indent=4, ensure_ascii= False)

4, json.load from the file read

With open ("Data.json", "R", encoding= "Utf-8") as F_load:    r_load = json.load (f_load) print (Type (r_load)) print (R _load) Output:<' dict '>'Beijing ': {' Chaoyang District ': [' Three Li Tun ', ' wangjing ', ' Guomao '], ' Haidian District ': [' Five crossing ', ' College Road ', ' post Factory Village '], ' Dongcheng District ': [' dongzhimen ', ' chongwenmen ', ' Wangfujing ']}, ' Shanghai ': {' Jingan ': [], ' Huangpu ': [], ' Hongkou ': []}}

Python JSON & Pickle 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.