歡迎您光臨本站 註冊首頁

python dict亂碼如何解決

←手機掃碼閱讀     ljg58026 @ 2020-06-10 , reply:0

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

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

 

輸出結果:

{'age': 18, 'no': 1121, 'name': 'lily', 'sex': 'xe5xa5xb3'}

解決方法:

  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會出現以下的結果:

  #打印字典  dict = {'name': '張三'}  print dict  >>>{'name': 'xe5xbcxa0xe4xb8x89'}     #打印列表  list = [{'name': '張三'}]  print list  >>>[{'name': 'xe5xbcxa0xe4xb8x89'}]

 

解決方案:

使用以下方法進行輸出:

  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': '張三'}]

                            

   


[ljg58026 ] python dict亂碼如何解決已經有238次圍觀

http://coctec.com/docs/python/shhow-post-237928.html