Python Tickle module and JSON module

Source: Internet
Author: User

#! /usr/bin/env python
#-*-Coding:utf-8-*-
#JSON (JavaScript object Notation, JS tag) is a lightweight data interchange format.
# The JSON data format is actually the dictionary format in Python, which can contain arrays enclosed in square brackets.
# in Python, there are modules--json and Picle modules that specialize in JSON format
# Python's pickle module implements basic data sequence and deserialization.
# through serialization of the Pickle module we are able to save the object information running in the program to a file.
# through the deserialization of the Pickle module, we are able to read the saved object from the file.
# Serialization: Dumps (object) returns a string, dumps can format all the basic data types as strings.
# Dump (object, file) writes an object to a file, which can be an actual physical file or any file-like object that has
# Write () method, which can accept a single string parameter
#
# Deserialization: Loads (string) returns an object contained in a JSON string
#
# load (file) returns the object contained in the pickle file

Import Pickle
Import JSON
data = {
' 1 ': {' name ': ' A ', ' pwd ': ' 1 ', ' tel ': ' 99976548 '},
"2": {"name": "B", "pwd": "2", "tel": "73628362"},
"3": [500,1000,1500]
}

########### #pickle写入文件 #################################################################
File = Open ("pi.pc", ' WB ')
Pickle.dump (Data,file)
File.close ()

################ #pickle读取文件 ############################################################
f = open ("pic.pc", ' RB ')
Data1 = Pickle.load (f)
Print (DATA1)

##################### #json写入文件 ######################################################
With open ("Pic.json", ' W ', encoding= ' utf-8 ') as F:
# Indent format save dictionary, default to none, less than 0 for 0 spaces
Json.dump (data,f,indent=4)

############### #json读取文件内容 ###############################################################
With open ("Pic.json", ' R ', encoding= ' utf-8 ') as FF:
obj = json.load (ff)
Print (obj)

Python Tickle module and JSON 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.