如何将阿拉伯文字从PyQt4转换为UTF-8 [英] How to convert Arabic text from PyQt4 to UTF-8
问题描述
我用PyQt4制作了一个Python 2 GUI应用程序,它有两个条目.第一个使用文件名,第二个使用文本写入文件.
I made a Python 2 GUI application with PyQt4 that has two entries. The first takes the file name, and the second takes the text to write in the file.
我想在两者中都输入阿拉伯文字,所以我写了这个函数:
I want to enter Arabic text in both of them, so I wrote this function:
def makefile(self):
self.name_file=str(self.lineEdit.text()).decode("utf-8")
self.string=str(self.lineEdit_2.text()).decode("utf-8")
file=open(self.name_file,"w")
file.write(self.string)
file.close()
当我输入英文字母时,它可以正常工作,但是当我输入阿拉伯文时,出现以下错误:
When I enter English letters it works fine, but when I enter Arabic I get the following error:
UnicodeEncodeError:'ascii'编解码器无法对位置0-2处的字符进行编码:序数不在范围(128)中
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
推荐答案
您不是编写将Unicode转换为UTF-8的代码,而是编写了将UTF-8转换为Unicode的代码.这就是您遇到的错误.
Instead of writing code to convert from your unicode to UTF-8, you wrote code to convert from UTF-8 to unicode. That's what you're getting errors.
decode("utf-8")
表示
采用UTF-8编码的二进制
str
并转换为unicode
字符串.
Take a UTF-8 encoded binary
str
and convert to aunicode
string.
相反,encode("utf-8")
表示
取一个
unicode
字符串,并使用UTF-8编码为二进制str
.
take a
unicode
string and encode into a binarystr
using UTF-8.
您似乎正在尝试将文本编码为UTF-8,因此可以使用UTF-8编码将其写入文件.因此,您应该使用encode()
而不是decode()
.
It looks like you're trying to encode text as UTF-8, so you can write it to your file in UTF-8 encoding. So you should use be using encode()
instead of decode()
.
此外,您要获取Unicode中的QString
值,并在其上调用str()
.这会尝试使用ASCII将其更改为二进制str
,这不适用于您的阿拉伯文本,并会导致您所看到的异常.无论如何,这不是您想做的—您想使用UTF-8,而不是ASCII.因此,请勿将其转换为二进制str
,而应使用unicode()
将其转换为unicode
对象.
Also, you're taking your QString
value, which is in unicode, and calling str()
on it. This attempts to change it to a binary str
using ASCII, which doesn't work for your Arabic text, and causes the exception you're seeing. And it's not what you wanted to do, anyway—you wanted to use UTF-8, not ASCII. So don't convert it to a binary str
, convert it to a unicode
object with unicode()
.
因此,例如,代替
str(self.lineEdit_2.text()).decode("utf-8")
您应该改写
unicode(self.lineEdit_2.text()).encode("utf-8")
这篇关于如何将阿拉伯文字从PyQt4转换为UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!