濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題

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

熱門(mén)標(biāo)簽:外呼不封號(hào)系統(tǒng) 悟空智電銷(xiāo)機(jī)器人6 荊州云電銷(xiāo)機(jī)器人供應(yīng)商 幫人做地圖標(biāo)注收費(fèi)算詐騙嗎 江蘇房產(chǎn)電銷(xiāo)機(jī)器人廠家 溫州旅游地圖標(biāo)注 遼寧400電話(huà)辦理多少錢(qián) 電信營(yíng)業(yè)廳400電話(huà)申請(qǐng) 蘇州電銷(xiāo)機(jī)器人十大排行榜

python3 默認(rèn)的是UTF-8格式,但在在用dump寫(xiě)入的時(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)

在打開(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(':')

更多關(guān)于解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)與亂碼問(wèn)題請(qǐng)查看下面的相關(guān)鏈接

您可能感興趣的文章:
  • Python 讀寫(xiě) Matlab Mat 格式數(shù)據(jù)的操作
  • python基于Pandas讀寫(xiě)MySQL數(shù)據(jù)庫(kù)
  • python里讀寫(xiě)excel等數(shù)據(jù)文件的6種常用方式(小結(jié))
  • python讀寫(xiě)數(shù)據(jù)讀寫(xiě)csv文件(pandas用法)
  • Python web框架(django,flask)實(shí)現(xiàn)mysql數(shù)據(jù)庫(kù)讀寫(xiě)分離的示例
  • python讀寫(xiě)excel數(shù)據(jù)--pandas詳解

標(biāo)簽:宿遷 黃山 欽州 臺(tái)灣 濟(jì)南 三沙 喀什 景德鎮(zhèn)

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題》,本文關(guān)鍵詞  解決,python3,json,數(shù)據(jù),包含,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于解決python3 json數(shù)據(jù)包含中文的讀寫(xiě)問(wèn)題的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    滕州市| 广宁县| 仙居县| 上杭县| 定兴县| 承德市| 民勤县| 基隆市| 黄平县| 枣阳市| 麟游县| 仪征市| 黑河市| 开阳县| 红桥区| 遵义县| 乳源| 体育| 霍州市| 永丰县| 汉川市| 安远县| 上高县| 成都市| 九寨沟县| 扎囊县| 鹤庆县| 若尔盖县| 深水埗区| 沙雅县| 林周县| 高唐县| 青川县| 漳州市| 邮箱| 佛山市| 镶黄旗| 双牌县| 兴海县| 同德县| 元谋县|