gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:11:01 25 4
gpt4 key购买 nike

下面的代码可以将字符串编码为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com