Python學習筆記06

2021-09-25 21:50:07 字數 1676 閱讀 4124

使用 json 函式需要匯入 json 庫:import json

json.dumps 用於將 python 物件編碼成 json 字串

json.dumps(obj, skipkeys=false, ensure_ascii=true, check_circular=true, allow_nan=true, cls=none, indent=none, separators=none, encoding="utf-8", default=none, sort_keys=false, **kw)
引數解釋:

>>> import json

>>> data = [ ]

>>> json=json.dumps(data)

>>> print json

>>> data

>>> print json.dumps(data, sort_keys=true, indent=2, separators=(',', ': '))[

]

json.loads 用於解碼 json 資料。該函式返回 python 欄位的資料型別

json.loads(s[, encoding[, cls[, object_hook[, parse_float[, parse_int[, parse_constant[, object_pairs_hook[, **kw]]]]]]]])
引數解釋:

>>> import json

>>> jdata=''

file "", line 1

jdata=''

^syntaxerror: invalid syntax

>>> import json

>>> jdata=''

>>> result=json.loads(jdata)

>>> print result

>>> result

>>> type(result)

>>> type(jdata)

python encode() 函式用於將 python 物件編碼成 json 字串

demjson.encode(self, obj, nest_level=0)
>>> import demjson

>>> data = [ ]

>>> result = demjson.encode(data)

>>> print result

>>> type(result)

>>> type(data)

python 可以使用 demjson.decode() 函式解碼 json 資料。該函式返回 python 欄位的資料型別

demjson.decode(self, txt)
>>> import demjson

>>> jdata=''

>>> result=demjson.decode(jdata)

>>> print result

>>> type(result)

>>> type(jdata)

python學習筆記06(讀寫檔案)

created on mon feb 12 00 18 30 2018 author myself f open data.txt 開啟檔案 data f.read 檔案內容寫入字串data print data 輸出字串 f.close 關閉檔案,釋放資源runfile f python list...

學習筆記06

do while 水仙花 從100到999 各個位數的立方和相加等於這個數本身就是乙個水仙花數 include intmain void i while i 999 return0 pow 用來計算以x為底的y次方值 include 上述式子可改為 if pow a,3 pow b,3 pow c,...

python學習筆記06 json操作

json就是一串字串 json只有雙引號 字典是單引號 1.字典轉成json 1 import json2 json模組作用 python的資料型別和json相互轉換的 3 d 45 字典轉成字串 json 6 json str json.dumps d,indent 4,ensure ascii ...