以前在 python2 中 a=bytes(bytearray((182,)))结果是 a='\xb6'
现在在 Python3 运行结果是 a=b'\xb6',尝试各种 a.decode()均失败了,请问如何能在 Python3 得到和 Python2 一样的结果呢?
现在在 Python3 运行结果是 a=b'\xb6',尝试各种 a.decode()均失败了,请问如何能在 Python3 得到和 Python2 一样的结果呢?
1
dikT Sep 12, 2017
str(a)
|
2
ThunderEX Sep 12, 2017 1. Python3 里面的 str 已经是 unicode str 了。
2. bytes(bytearray((182,)))你用的就是 bytes 函数,所以理所当然的,无论 py2 还是 py3 都返回 bytes 类型,只不过 py2 时 bytes==str 而已。 3. 如果你想得到的是 latin_1 编码的\xb6 代表的 str,请用.decode('latin-1') |
4
gogobody OP @ThunderEX 再请教一下,在 Python2 和 Python3 中用 chr()函数出的结果不一样,有什么替代方案吗
|
5
ThunderEX Sep 12, 2017
如果你要的是 unicode str,那就是 chr(250),如果你要的是 latin-1 格式编码,那就是 chr(250).encode('latin-1')
|