python字典中鍵值對的值為中文,打印成轉義字符,怎么解決


今天是2019-10-02,學習代碼第二天.

python字典中鍵值對中有中文,打印的時候,發現成轉義字符了.查了好久,解決.記錄一下.useful.

今日份代碼:

 1 #!/usr/bin/python
 2 # -*- coding:utf-8 -*-
 3 # 字典和列表的區別
 4 # 列表是有序的對象集合,字典是無序的對象集合
 5 # 字典的特點,字典用{}定義,使用鍵值對存儲數據,鍵值對間用,分開
 6 # 鍵--key,索引  值--value,數據   鍵和值間用:分開   鍵--唯一的,只能取字符串/數字/元組   值--可以任意類型
 7 name = "小馬蹄"
 8 print(name)
 9 xiaomati = {"name":name,
10           "age": 26,
11           "gender": "male",
12           "height": 1.71,
13           "weight": 67,
14           "skin": "yellow and black"}
15 print(xiaomati)
16 
17 decoded_data = repr(xiaomati).decode('string_escape')
18 print decoded_data

主要是第17行的repr(**).decode('string_escape')發揮了真正的威力.最后控制台的輸出:

 小馬蹄
{'name': '\xe5\xb0\x8f\xe9\xa9\xac\xe8\xb9\x84', 'weight': 67, 'gender': 'male', 'age': 26, 'height': 1.71, 'skin': 'yellow and black'}
{'name': '小馬蹄', 'weight': 67, 'gender': 'male', 'age': 26, 'height': 1.71, 'skin': 'yellow and black'}

開始搜索了強制轉換那些方法,可能是python字典的方式不支持.

哦,我的編譯環境是python2.7,python3.5沒這樣.

來自小白的淡淡憂傷.希望自己有天也能成長為大牛.

每天敲一些代碼,記錄自己遇到的問題,也是一種進步.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM