Two modules for serialization in Python
- JSON is used to convert between "string" and "Python Basic data Types"
- Pickle for "Python-specific type" and "Python basic data type" to convert between
The JSON module provides four functions: dumps, dump, loads, load
The Pickle module provides four functions: dumps, dump, loads, load
Json.dudmp ("[11,22]", open (' Db.txt ', ' W ')) #转换成字符串再保存到该文件.
Json.load (Open (' Db.txt ', ' R ')) #打开该文件装成字符串.
Import JSON
s= "{" K1 ":" 123 "}" #注, the characters in the dictionary must be enclosed in double quotation marks before they can be converted to JSON.
L="[11,22,33,44]"= json.loads (1) #用于将字典, list, tuple form strings into dictionaries, lists, tuples.
st = Json.dumps (l) # Converts the list to a JSON-formatted string.
Print (Result,type (Result))
Receives a value from an interface. py
Import urllib.request Import JSON req = Urllib.request.Request ('http://www.baidu.com/api ")r == str (R.read (), encoding=" utf-8"= json.load (reesult)print (DIC)
Python cross-language data interaction, Json,pickle (serialization).