1. 程式人生 > >day5-Python學習筆記(九)json數據類型

day5-Python學習筆記(九)json數據類型

class json mps pen log imp color python 文件

import json

#json串就是字符串。

d = {
‘car‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
‘挨糞叉‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
‘挨糞叉1‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
‘挨糞叉2‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
‘挨糞叉3‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
‘挨糞叉4‘:{‘color‘:‘red‘,‘price‘:100,‘count‘:50},
}

# res = json.dumps(d,indent=8,ensure_ascii=False) #把list、字典轉成json,indent多少縮進,ensure_ascii可以顯示中文
# f1 = open(‘f1‘,‘w‘,encoding=‘utf-8‘)
# f1.write(res)

# f1 = open(‘f1‘,encoding=‘utf-8‘)
# res = f1.read()
# dict_res = json.loads(res) #把json串變成python的數據類型
# print(dict_res)

# f1 = open(‘f1‘,‘w‘,encoding=‘utf-8‘)
# json.dump(d,f1,ensure_ascii=False,indent=4)
#自動幫你寫入文件,第一個參數是數據,第二個是文件對象

f1 = open(‘f1‘,encoding=‘utf-8‘)
print(json.load(f1))
#自動幫你讀文件。

day5-Python學習筆記(九)json數據類型