国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題

2020-01-04 15:07:29
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

python3 默認(rèn)的是UTF-8格式,但在在用dump寫(xiě)入的時(shí)候仍然要注意:如下

import jsondata1 = { "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)

在打開(kāi)文件的時(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碼寫(xiě)到文件中,如下:

{ "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文件寫(xiě)中文的時(shí)候也要注意在打開(kāi)的時(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(':')

以上這篇解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到python教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 白玉县| 岳阳县| 宝山区| 吉林省| 上栗县| 白水县| 五华县| 宕昌县| 小金县| 北票市| 平泉县| 昌邑市| 临颍县| 莱州市| 枝江市| 沂水县| 桦川县| 岳西县| 万荣县| 澄江县| 镇平县| 砚山县| 永嘉县| 冕宁县| 扶余县| 蕉岭县| 虞城县| 乌拉特中旗| 丰镇市| 临朐县| 美姑县| 岗巴县| 宜黄县| 霍邱县| 勃利县| 曲麻莱县| 西乡县| 汝南县| 五寨县| 太康县| 临澧县|