天天看點

python2.x版本出現Unicode中文字元\xe5\xa5\xb3\xe5\xa3\xab\xef\xbc\x8c\xe6\x82\xa8\xe5\xa5\xbd',,怎樣轉換為中文

在Python2.x版本中,已加了

#-*-coding:utf-8-*-程式運作結果出現的不是亂碼,而是一些Unicode字元,('\xe5\xa5\xb3\xe5\xa3\xab\xef\xbc\x8c\xe6\x82\xa8\xe5\xa5\xbd)這屬于Python2.x版本裡的問題,是Unicode字元在記憶體中的形式,因為該版本不會自動轉碼。我目前嘗試了一下。
      

方法一:

print函數分開列印。

print(str1)

print(str2)

方法二:

采用print(str1+str2)形式,不要采用print(str1,str2)形式,前者會預設為一個字元串的拼接,而後者是一個元組形式。

在python中進行編碼轉換都是通過unicode作為中間值實作的。是以要先decode成unicode字元,然後再使用encode轉換成utf-8編碼的str。

采用print(str1,str2).decode("ascii").encode("utf-8") 出現如下錯誤

 Traceback (most recent call last):

  File "G:/pycharm/test/��֬���Ż�py", line 32, in <module>

    print((wenhao , notice).decode("ascii").encode('utf-8'))

AttributeError: 'tuple' object has no attribute 'decode'

此處還沒有解決方案。以後再補充。