JSON and Pickle

Source: Internet
Author: User

Two modules for serialization

JSON: Used to convert between string and Python data types

Pickle: Converting 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

Pickle instances:

  Import Pickle
  data = {' K1 ': 123, ' K2 ': ' Hello '}
  Pickle. Dumps converts data to a string that can be recognized only by Python speech in a special form
  Pickle. dumps (data)
  Print P_str
  6: "'
  7: Print the result:
  8: (dp0
  9:s ' K2 '
10:p1
11:s ' Hello '
12:p2
13:ss ' K1 '
14:p3
15:i123
16:s.
17: "'
Pickle. dump transforms the data into a string that is known only to the Python voice in a special form and writes the file
Open (' e:/python/tmp/result.pk ', ' W ') as FP:
:     pickle.  Dump(data, FP)

JSON instance

  Import JSON
  data = {' K1 ': 123, ' K2 ': ' Hello '}
  3: # JSON. dumps converts data into strings that are known to all programming languages in a special form
  4:J_STR = json. dumps (data)
  Print J_str
  6: # Print Result: {"K2": "Hello", "K1": 123}
  7: # JSON. dump transforms the data into a string that is known to all languages in a special form and writes the file
  Open (' e:/python/tmp/result.pk ', ' W ') as JP:
  9:     JSON.  Dump(data, JP)

Configparser

  1: #!/usr/bin/env python
  2: #-*-Coding:utf-8-*-
  3: # to operate on a specific configuration, the name of the current module is changed to Configparser in the Python 3.x version.
  Import Configparser
  5:config = Configparser.configparser ()
  6:config. Read (' Goods.txt ')
  
  8: # Get the name of the module
  9:secs = Config.sections ()
Print secs
11: # Result: [' section1 ', ' Section2 ']
12: # Gets the key value of the specified module
13:options = config.options (' Section1 ')
Print Options
15: # Result: [' K1 ', ' K2 ']
16: # Gets the items under the specified module
17:item_list = Config.items (' Section1 ')
Print item_list
19: # Result: [(' K1 ', ' v1 '), (' K2 ', ' V2 ')]
20: # Gets the value of the key under the specified module
21:val = Config.get (' Section1 ', ' K2 ')
Print val

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.