詳解python讀寫json文件
python處理json文本文件主要是以下四個(gè)函數(shù):
| 函數(shù) | 作用 |
|---|---|
| json.dumps | 對(duì)數(shù)據(jù)進(jìn)行編碼,將python中的字典 轉(zhuǎn)換為 字符串 |
| json.loads | 對(duì)數(shù)據(jù)進(jìn)行解碼,將 字符串 轉(zhuǎn)換為 python中的字典 |
| json.dump | 將dict數(shù)據(jù)寫入json文件中 |
| json.load | 打開json文件,并把字符串轉(zhuǎn)換為python的dict數(shù)據(jù) |
json.dumps / json.loads
數(shù)據(jù)轉(zhuǎn)換對(duì)照:
| json | python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
| true | True |
| false | False |
| null | None |
代碼示例:
import json
tesdic = {
'name': 'Tom',
'age': 18,
'score':
{
'math': 98,
'chinese': 99
}
}
print(type(tesdic))
json_str = json.dumps(tesdic)
print(json_str)
print(type(json_str))
newdic = json.loads(json_str)
print(newdic)
print(type(newdic))
輸出為:
<class 'dict'>
{"name": "Tom", "age": 18, "score": {"math": 98, "chinese": 99}}
<class 'str'>
{'name': 'Tom', 'age': 18, 'score': {'math': 98, 'chinese': 99}}
<class 'dict'>
json.dump / json.load
寫入json的內(nèi)容只能是dict類型,字符串類型的將會(huì)導(dǎo)致寫入格式問題:
with open("res.json", 'w', encoding='utf-8') as fw:
json.dump(json_str, fw, indent=4, ensure_ascii=False)
則json文件內(nèi)容為:
"{\"name\": \"Tom\", \"age\": 18, \"score\": {\"math\": 98, \"chinese\": 99}}"
我們換一種數(shù)據(jù)類型寫入:
with open("res.json", 'w', encoding='utf-8') as fw:
json.dump(tesdic, fw, indent=4, ensure_ascii=False)
則生成的josn就是正確的格式:
{
"name": "Tom",
"age": 18,
"score": {
"math": 98,
"chinese": 99
}
}
同理,從json中讀取到的數(shù)據(jù)也是dict類型:
with open("res.json", 'r', encoding='utf-8') as fw:
injson = json.load(fw)
print(injson)
print(type(injson))
{'name': 'Tom', 'age': 18, 'score': {'math': 98, 'chinese': 99}}
<class 'dict'>
總結(jié)
本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Pytorch中的masked_fill基本知識(shí)詳解
本文介紹了PyTorch中masked_fill函數(shù)的基本使用和原理,該函數(shù)接受一個(gè)輸入張量和一個(gè)布爾掩碼作為參數(shù),掩碼的形狀必須與輸入張量相同,True表示需要填充的位置,False表示保持原值2024-10-10
Python實(shí)現(xiàn)企業(yè)微信通知機(jī)器人的方法詳解
這篇文章主要為大家詳細(xì)介紹了如何使用Python實(shí)現(xiàn)對(duì)企業(yè)微信進(jìn)行群通知的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02
opencv+python實(shí)現(xiàn)均值濾波
這篇文章主要為大家詳細(xì)介紹了opencv+python實(shí)現(xiàn)均值濾波,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
Python 在 VSCode 中使用 IPython Kernel 的方法詳解
這篇文章主要介紹了Python 在 VSCode 中使用 IPython Kernel 的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
在Python中操作字符串之startswith()方法的使用
這篇文章主要介紹了在Python中操作字符串之startswith()方法的使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05
Python文件操作,open讀寫文件,追加文本內(nèi)容實(shí)例
本篇文章主要介紹了Python文件操作,open讀寫文件,追加文本內(nèi)容,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12
在macOS上搭建python環(huán)境的實(shí)現(xiàn)方法
今天小編就為大家分享一篇在macOS上搭建python環(huán)境的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
關(guān)于Pandas?count()與values_count()的用法及區(qū)別
這篇文章主要介紹了關(guān)于Pandas?count()與values_count()的用法及區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05
tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法
今天小編就為大家分享一篇tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07

