python可以对utf-8编码,但不能解码。

时间:2023-01-04 20:01:28

The Code Below Can Encode A String To Utf-8 :

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

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

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

That Prints:

打印:

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

But I can't Decode This String With This Code :

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

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

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

The error is:

错误的是:

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'

Please Help Me ...

请帮我…

Edit

From the answers switched to a byte string:

从答案切换到一个字节字符串:

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

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

Now the error is:

现在的错误是:

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>

3 个解决方案

#1


4  

It looks like you're using Python 3.X. You .encode() Unicode strings (u'xxx' or 'xxx'). You .decode() byte strings b'xxxx'.

看起来你用的是python3。x。编码()Unicode字符串(u'xxx'或'xxx')。解码()字节字符串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'))

Note your terminal may not be able to display the Unicode string. Mine Windows console doesn't:

注意,您的终端可能无法显示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>

But it does do the decode. '\uxxxx' represents a Unicode code point.

但它确实做了解码。'\uxxxx'表示Unicode代码点。

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

My PythonWin IDE supports UTF-8 and can display the characters:

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

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

You can also write the data to a file and display it in an editor that supports UTF-8, like Notepad. since your original string is already UTF-8, just write it to a file directly as bytes. 'wb' opens the file in binary mode and the bytes are written as is:

您还可以将数据写入文件,并在支持UTF-8的编辑器中显示,就像记事本。因为您的原始字符串已经是UTF-8,所以直接将它写到文件中作为字节。“wb”以二进制模式打开文件,字节为:

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

If you have a Unicode string, you can write it as UTF-8 with:

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

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

P.S. str is a built-in type. Don't use it for variable names.

str是一种内置类型。不要为变量名使用它。

Python 2.x works differently. 'xxxx' is a byte string and u'xxxx' is a Unicode string, but you still .encode() the Unicode string and .decode() the byte string.

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

#2


0  

Use following code:

使用以下代码:

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

#3


0  

Python has a first class unicode type that you can use in place of the plain bytestring str type. It’s easy, once you accept the need to explicitly convert between a bytestring and a Unicode string:

Python有一个第一个类unicode类型,您可以使用它代替普通的bytestring str类型。一旦您接受了需要显式地在bytestring和Unicode字符串之间进行转换,这很简单:

>>> persian_enter = unicode('\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf', 'utf8')
>>> print persian_enter
ورود

Python 2 had two global functions to coerce objects into strings: unicode() to coerce them into Unicode strings, and str() to coerce them into non-Unicode strings. Python 3 has only one string type, Unicode strings, so the str() function is all you need. (The unicode() function no longer exists.)

Python 2有两个全局函数将对象强制转换为字符串:unicode()将它们强制转换为unicode字符串,并将它们强制转换为非unicode字符串。Python 3只有一个字符串类型,即Unicode字符串,因此,str()函数是您所需要的。(unicode()函数不再存在。)

read more about reading and writing unicode data

阅读更多有关阅读和书写unicode数据。

#1


4  

It looks like you're using Python 3.X. You .encode() Unicode strings (u'xxx' or 'xxx'). You .decode() byte strings b'xxxx'.

看起来你用的是python3。x。编码()Unicode字符串(u'xxx'或'xxx')。解码()字节字符串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'))

Note your terminal may not be able to display the Unicode string. Mine Windows console doesn't:

注意,您的终端可能无法显示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>

But it does do the decode. '\uxxxx' represents a Unicode code point.

但它确实做了解码。'\uxxxx'表示Unicode代码点。

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

My PythonWin IDE supports UTF-8 and can display the characters:

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

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

You can also write the data to a file and display it in an editor that supports UTF-8, like Notepad. since your original string is already UTF-8, just write it to a file directly as bytes. 'wb' opens the file in binary mode and the bytes are written as is:

您还可以将数据写入文件,并在支持UTF-8的编辑器中显示,就像记事本。因为您的原始字符串已经是UTF-8,所以直接将它写到文件中作为字节。“wb”以二进制模式打开文件,字节为:

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

If you have a Unicode string, you can write it as UTF-8 with:

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

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

P.S. str is a built-in type. Don't use it for variable names.

str是一种内置类型。不要为变量名使用它。

Python 2.x works differently. 'xxxx' is a byte string and u'xxxx' is a Unicode string, but you still .encode() the Unicode string and .decode() the byte string.

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

#2


0  

Use following code:

使用以下代码:

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

#3


0  

Python has a first class unicode type that you can use in place of the plain bytestring str type. It’s easy, once you accept the need to explicitly convert between a bytestring and a Unicode string:

Python有一个第一个类unicode类型,您可以使用它代替普通的bytestring str类型。一旦您接受了需要显式地在bytestring和Unicode字符串之间进行转换,这很简单:

>>> persian_enter = unicode('\xd9\x88\xd8\xb1\xd9\x88\xd8\xaf', 'utf8')
>>> print persian_enter
ورود

Python 2 had two global functions to coerce objects into strings: unicode() to coerce them into Unicode strings, and str() to coerce them into non-Unicode strings. Python 3 has only one string type, Unicode strings, so the str() function is all you need. (The unicode() function no longer exists.)

Python 2有两个全局函数将对象强制转换为字符串:unicode()将它们强制转换为unicode字符串,并将它们强制转换为非unicode字符串。Python 3只有一个字符串类型,即Unicode字符串,因此,str()函数是您所需要的。(unicode()函数不再存在。)

read more about reading and writing unicode data

阅读更多有关阅读和书写unicode数据。