解決python3 json數(shù)據(jù)包含中文的讀寫問題
python3 默認(rèn)的是UTF-8格式,但在在用dump寫入的時(shí)候仍然要注意:如下
import json data1 = { "TestId": "testcase001", "Method": "post", "Title": "登錄測(cè)試", "Desc": "登錄基準(zhǔn)測(cè)試", "Url": "http://xxx.xxx.xxx.xx", "InputArg": { "username": "王小丫", "passwd": "123456", }, "Result": { "errorno": "0" } } with open('casedate.json', 'w', encoding='utf-8') as f: json.dump(data1, f, sort_keys=True, indent=4)
在打開文件的時(shí)候要加上encoding=‘utf-8',不然會(huì)顯示成亂碼,如下:
{ "Desc": "��¼������", "InputArg": { "passwd": "123456", "username": "��СѾ" }, "Method": "post", "Result": { "errorno": "0" }, "TestId": "testcase001", "Title": "��¼����", "Url": "http://xxx.xxx.xxx.xx" }
在dump的時(shí)候也加上ensure_ascii=False,不然會(huì)變成ascii碼寫到文件中,如下:
{ "Desc": "\u767b\u5f55\u57fa\u51c6\u6d4b\u8bd5", "InputArg": { "passwd": "123456", "username": "\u738b\u5c0f\u4e2b" }, "Method": "post", "Result": { "errorno": "0" }, "TestId": "testcase001", "Title": "\u767b\u5f55\u6d4b\u8bd5", "Url": "http://xxx.xxx.xxx.xx" }
另外python3在向txt文件寫中文的時(shí)候也要注意在打開的時(shí)候加上encoding=‘utf-8',不然也是亂碼,如下:
with open('result.txt', 'a+', encoding='utf-8') as rst: rst.write('return data') rst.write('|') for x in r.items(): rst.write(x[0]) rst.write(':')
更多關(guān)于解決python3 json數(shù)據(jù)包含中文的讀寫與亂碼問題請(qǐng)查看下面的相關(guān)鏈接
相關(guān)文章
tensorflow 中對(duì)數(shù)組元素的操作方法
今天小編就為大家分享一篇tensorflow 中對(duì)數(shù)組元素的操作方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07windows+vscode安裝paddleOCR運(yùn)行環(huán)境的步驟
這篇文章主要介紹了windows+vscode安裝paddleOCR運(yùn)行環(huán)境,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11在python 中split()使用多符號(hào)分割的例子
今天小編就為大家分享一篇在python 中split()使用多符號(hào)分割的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-07-07代碼解析python標(biāo)準(zhǔn)庫logging模塊
這篇文章主要為大家介紹了代碼解析python標(biāo)準(zhǔn)庫logging模塊,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05python實(shí)現(xiàn)名片管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)名片管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11python pandas 對(duì)series和dataframe的重置索引reindex方法
今天小編就為大家分享一篇python pandas 對(duì)series和dataframe的重置索引reindex方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-06-06Python趣味實(shí)例,實(shí)現(xiàn)一個(gè)簡(jiǎn)單的抽獎(jiǎng)刮刮卡
這篇文章主要介紹了Python如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的抽獎(jiǎng)刮刮卡,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07