欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python dict亂碼如何解決

 更新時間:2020年06月07日 15:34:54   作者:yang  
在本篇文章里小編給大家分享了關于python dict亂碼解決方法,需要的朋友們可以參考下。

定義字典并直接輸出,結果輸出結果中文是亂碼展示

d={'name':'lily','age':18,'sex':'女','no':1121}
print d

輸出結果:

{'age': 18, 'no': 1121, 'name': 'lily', 'sex': '\xe5\xa5\xb3'}

解決方法:

d={'name':'lily','age':18,'sex':'女','no':1121}
print json.dumps(d,encoding='utf-8',ensure_ascii=False)

輸出結果:

{"age": 18, "no": 1121, "name": "lily", "sex": "女"}

內容擴展:

Python中列表或字典輸出亂碼的解決方法

問題: Python中的列表(list)或字典包含中文字符串,直接使用print會出現(xiàn)以下的結果:

#打印字典
dict = {'name': '張三'}
print dict
>>>{'name': '\xe5\xbc\xa0\xe4\xb8\x89'}
 
#打印列表
list = [{'name': '張三'}]
print list
>>>[{'name': '\xe5\xbc\xa0\xe4\xb8\x89'}]

解決方案:

使用以下方法進行輸出:

import json
 
#打印字典
dict = {'name': '張三'}
print json.dumps(dict, encoding="UTF-8", ensure_ascii=False)
>>>{'name': '張三'}
 
#打印列表
list = [{'name': '張三'}]
print json.dumps(list, encoding="UTF-8", ensure_ascii=False)
>>>[{'name': '張三'}]

到此這篇關于python dict亂碼如何解決的文章就介紹到這了,更多相關python dict亂碼解決方法內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

最新評論