用于用户的 Unicode 阿拉伯字符串
Unicode arabic string to user it
我有一个变量,它的值类似于 x='مصطفى'
,我想将它转换为 u'مصطفى'
的形式,以便在某些函数中再次使用它.. 当我尝试做 u''+x
它总是给我一个错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
有什么帮助吗?
谢谢我解决了:)
解决办法就是这样做
u''.encode('utf-8')+x
有两件事。
首先,x='مصطفى'
的含义定义不明确,如果您将源文件保存为另一种编码,则会发生变化。另一方面,x=u'مصطفى'.encode('utf-8')
明确表示“使用 UTF-8 编码该文本时得到的字节”。
其次,使用字节 'abc'
或 b'abc'
或 unicode u'abc'
,但不要混合使用它们。将它们混合在 python 2.x 中产生的结果取决于您执行该代码的位置。在 python 3.x 中它会引发错误(有充分的理由)。
所以给定一个字节串x
,或者:
# bytes
'' + x
或:
# unicode, so decode the byte string
u'' + x.decode('utf-8')
您必须知道这些字节的编码方式,然后 .decode(encoding)
它们才能获得 Unicode 字符串。如果您从 API 那里收到它们,utf8
是一个很好的猜测。如果您从 Windows 记事本中键入的文件中读取字节,则更有可能是一些阿拉伯语 (?) 代码页。
PythonWin 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32.
>>> x='مصطفى' # "Just bytes" in whatever encoding my console uses
>>> x # Looks like UTF-8.
'\xd9\x85\xd8\xb5\xd8\xb7\xd9\x81\xd9\x89'
>>> x.decode('utf8') # Success
u'\u0645\u0635\u0637\u0641\u0649'
>>> print(x.decode('utf8'))
مصطفى
python 中有两个名为 python-bidi
和 arabic_reshaper
的库,使用它们您可以毫无问题地编写阿拉伯语文本或隐藏字母或单独的字母等。
通过在终端中输入来下载它们:pip install python-bidi, arabic_reshaper
示例:
import bidi.algorithm, arabic_reshaper
# To get arabic outputs in terminal or kivy or even pyGame etc.
reshaper = arabic_reshaper.reshape("أهلا وسهلا بكم")
bidi_text = bidi.algorithm.get_display(reshaper)
# "bidi_text" above makes python read from right to left like arabic language
print(bidi_text)
# Result in terminal:
>>>أهلا وسهلا بكم
# To append arabic text in a text file:
File = open('av.txt', 'w',encoding='utf-8')
File.write(reshaper)
我有一个变量,它的值类似于 x='مصطفى'
,我想将它转换为 u'مصطفى'
的形式,以便在某些函数中再次使用它.. 当我尝试做 u''+x
它总是给我一个错误:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd9 in position 0: ordinal not in range(128)
有什么帮助吗?
谢谢我解决了:)
解决办法就是这样做
u''.encode('utf-8')+x
有两件事。
首先,x='مصطفى'
的含义定义不明确,如果您将源文件保存为另一种编码,则会发生变化。另一方面,x=u'مصطفى'.encode('utf-8')
明确表示“使用 UTF-8 编码该文本时得到的字节”。
其次,使用字节 'abc'
或 b'abc'
或 unicode u'abc'
,但不要混合使用它们。将它们混合在 python 2.x 中产生的结果取决于您执行该代码的位置。在 python 3.x 中它会引发错误(有充分的理由)。
所以给定一个字节串x
,或者:
# bytes
'' + x
或:
# unicode, so decode the byte string
u'' + x.decode('utf-8')
您必须知道这些字节的编码方式,然后 .decode(encoding)
它们才能获得 Unicode 字符串。如果您从 API 那里收到它们,utf8
是一个很好的猜测。如果您从 Windows 记事本中键入的文件中读取字节,则更有可能是一些阿拉伯语 (?) 代码页。
PythonWin 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32.
>>> x='مصطفى' # "Just bytes" in whatever encoding my console uses
>>> x # Looks like UTF-8.
'\xd9\x85\xd8\xb5\xd8\xb7\xd9\x81\xd9\x89'
>>> x.decode('utf8') # Success
u'\u0645\u0635\u0637\u0641\u0649'
>>> print(x.decode('utf8'))
مصطفى
python 中有两个名为 python-bidi
和 arabic_reshaper
的库,使用它们您可以毫无问题地编写阿拉伯语文本或隐藏字母或单独的字母等。
通过在终端中输入来下载它们:pip install python-bidi, arabic_reshaper
示例:
import bidi.algorithm, arabic_reshaper
# To get arabic outputs in terminal or kivy or even pyGame etc.
reshaper = arabic_reshaper.reshape("أهلا وسهلا بكم")
bidi_text = bidi.algorithm.get_display(reshaper)
# "bidi_text" above makes python read from right to left like arabic language
print(bidi_text)
# Result in terminal:
>>>أهلا وسهلا بكم
# To append arabic text in a text file:
File = open('av.txt', 'w',encoding='utf-8')
File.write(reshaper)