python的Json模塊提供了四個(gè)功能:
dumps、dump、loads、load
pickle模塊提供了四個(gè)功能:
dumps、dump、loads、load
jsondumps把數(shù)據(jù)類型轉(zhuǎn)換成字符串dump把數(shù)據(jù)類型轉(zhuǎn)換成字符串并存儲在文件中l(wèi)oads把字符串轉(zhuǎn)換成數(shù)據(jù)類型load把文件打開從字符串轉(zhuǎn)換成數(shù)據(jù)類型。
json是可以在不同語言之間交換數(shù)據(jù)的,而pickle只在python之間使用。json只能序列化最基本的數(shù)據(jù)類型,josn只能把常用的數(shù)據(jù)類型序列化(列表、字典、列表、字符串、數(shù)字、),比如日期格式、類對象!josn就不行了。而pickle可以序列化所有的數(shù)據(jù)類型,包括類,函數(shù)都可以序列化。
實(shí)例
dumps:將python中的字典轉(zhuǎn)換為字符串
importjson
test_dict={'bigberg':[7600,{1:[['iPhone',6300],['Bike',800],['shirt',300]]}]}
print(test_dict)
print(type(test_dict))
#dumps將數(shù)據(jù)轉(zhuǎn)換成字符串
json_str=json.dumps(test_dict)
print(json_str)
print(type(json_str))
loads:將字符串轉(zhuǎn)換為字典
1new_dict=json.loads(json_str)
2print(new_dict)
3print(type(new_dict))
以上內(nèi)容為大家介紹了python培訓(xùn)之怎么處理json,希望對大家有所幫助,如果想要了解更多Python相關(guān)知識,請關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。