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

首頁 > 編程 > Python > 正文

解決python3 json數據包含中文的讀寫問題

2020-02-23 00:04:38
字體:
來源:轉載
供稿:網友

python3 默認的是UTF-8格式,但在在用dump寫入的時候仍然要注意:如下

import jsondata1 = { "TestId": "testcase001", "Method": "post", "Title": "登錄測試", "Desc": "登錄基準測試", "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)

在打開文件的時候要加上encoding=‘utf-8',不然會顯示成亂碼,如下:

{ "Desc": "��¼��׼����", "InputArg": {  "passwd": "123456",  "username": "��СѾ" }, "Method": "post", "Result": {  "errorno": "0" }, "TestId": "testcase001", "Title": "��¼����", "Url": "http://xxx.xxx.xxx.xx"}

在dump的時候也加上ensure_ascii=False,不然會變成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文件寫中文的時候也要注意在打開的時候加上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數據包含中文的讀寫問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 连平县| 抚松县| 文成县| 长葛市| 久治县| 余庆县| 伊宁县| 永年县| 拉孜县| 中牟县| 金阳县| 巴里| 罗江县| 邢台县| 三台县| 昆山市| 朝阳市| 双柏县| 湖北省| 饶河县| 宜城市| 中江县| 江川县| 宝丰县| 苗栗市| 莲花县| 东莞市| 义马市| 佳木斯市| 洛川县| 宜君县| 上饶市| 诏安县| 马公市| 绵竹市| 丹凤县| 班戈县| 上林县| 宜兰县| 日土县| 田阳县|