python3 默认的是UTF-8格式,但在在用dump写入的时候仍然要注意:如下
importjson data1={ "TestId":"testcase001", "Method":"post", "Title":"登录测试", "Desc":"登录基准测试", "Url":"http://xxx.xxx.xxx.xx", "InputArg":{ "username":"王小丫", "passwd":"123456", }, "Result":{ "errorno":"0" } } withopen('casedate.json','w',encoding='utf-8')asf: 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" }
withopen('result.txt','a+',encoding='utf-8')asrst: rst.write('returndata') rst.write('|') forxinr.items(): rst.write(x[0]) rst.write(':')原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容