#! /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