python可以编码为utf-8但不能解码

标签 python python-3.x utf-8 decode

下面的代码可以将字符串编码为 Utf-8:

#!/usr/bin/python
# -*- coding: utf-8 -*-

str = 'ورود'
print(str.encode('utf-8'))

打印:

b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'

但是我不能用这段代码解码这个字符串:

#!/usr/bin/python
# -*- coding: utf-8 -*-

str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))

错误是:

Traceback (most recent call last):
  File "C:\test.py", line 5, in <module>
    print(str.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'

请帮帮我...

编辑

从答案切换到字节串:

#!/usr/bin/python
# -*- coding: utf-8 -*-

str = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
print(str.decode('utf-8'))

现在错误是:

Traceback (most recent call last):
  File "C:\test.py", line 5, in <module>
    print(str.decode('utf-8'))
  File "C:\Python34\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>

最佳答案

看起来您使用的是 Python 3.X。您 .encode() Unicode 字符串(u'xxx''xxx')。你 .decode() 字节串 b'xxxx'.

#!/usr/bin/python
# -*- coding: utf-8 -*-

s = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
#   ^
#   Need a 'b'
#
print(s.decode('utf-8'))

请注意,您的终端可能无法显示 Unicode 字符串。我的 Windows 控制台没有:

Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
>>> #   ^
... #   Need a 'b'
... #
... print(s.decode('utf-8'))
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "D:\dev\Python33x64\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-3: character maps to <undefined>

但它确实进行了解码。 '\uxxxx'代表一个Unicode代码点。

>>> s.decode('utf-8')
'\u0648\u0631\u0648\u062f'

我的PythonWin IDE支持UTF-8,可以显示字符:

>>> s = b'\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf'
>>> print(s.decode('utf-8'))
ورود

您还可以将数据写入文件,然后在支持 UTF-8 的编辑器(如记事本)中显示。由于您的原始字符串已经是 UTF-8,因此只需将其作为字节直接写入文件即可。 'wb' 以二进制模式打开文件,字节按原样写入:

>>> with open('out.txt','wb') as f:
...     f.write(s)

如果你有一个 Unicode 字符串,你可以将它写成 UTF-8:

>>> with open('out.txt','w',encoding='utf8') as f:
...     f.write(u)  # assuming "u" is already a decoded Unicode string.

附言str 是内置类型。不要将它用于变量名。

Python 2.x 的工作方式有所不同。 'xxxx' 是一个字节串而 u'xxxx' 是一个 Unicode 字符串,但你仍然 .encode() Unicode 字符串和 .decode() 字节串。

关于python可以编码为utf-8但不能解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26670103/

相关文章:

python - 删除时区(+01 :00) from DateTime

python - 如何在 python 中使用 ElementTree 访问包含命名空间的 xml 中的属性值

json - 在Python中,如何简洁地替换json数据中的嵌套值?

python - 无法访问 Python 模块

node.js - pg-promise UTF 连接字符串

mysql - 当 R 中的字符串包含 UTF-8 字符时,DBI 出现查询问题

python - Python 中使用了哪种正则表达式风格?

python - 强制 Nosetests 使用 Python 2.7 而不是 3.4

python-3.x - 使用 Python 将日文字符输出到文件

python距离公式坐标平面误差