Python-json and Pickle

Source: Internet
Author: User

This is a two module 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:

  1:import Pickle
  2:data = {' K1 ': 123, ' K2 ': ' Hello '}
  3: # Pickle.dumps converts data to a string that can be recognized only by Python speech in a special form
  4:P_STR = pickle.dumps (data)
  5: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 converts data into a string that is known only by Python speech in a special form and writes to a file
19:with open (' e:/python/tmp/result.pk ', ' W ') as FP:
:     pickle.dump (DATA,FP)

JSON instance

  1:import JSON
  2:data = {' K1 ': 123, ' K2 ': ' Hello '}
  3: # Json.dumps converts data to a string that is recognized by all programming languages in a special form
  4:J_STR = json.dumps (data)
  5:print J_str
  6: # Print Result: {"K2": "Hello", "K1": 123}
  7: # Json.dump converts data into a string that is recognized by all languages in a special form and writes to a file
  8:with 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.
  4:import Configparser
  5:config = Configparser.configparser ()
  6:config.read (' Goods.txt ')
  
  8: # Get the name of the module
  9:secs = Config.sections ()
10:print secs
11: # Result: [' section1 ', ' Section2 ']
12: # Gets the key value of the specified module
13:options = config.options (' Section1 ')
14:print Options
15: # Result: [' K1 ', ' K2 ']
16: # Gets the items under the specified module
17:item_list = Config.items (' Section1 ')
18: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 ')
22:print Val

Python-json and Pickle

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.