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'

此处还没有解决方案。以后再补充。

猜你喜欢

转载自blog.csdn.net/qq_38513966/article/details/81134565