如何将阿拉伯文本从 PyQt4 转换为 UTF-8 [英] How to convert Arabic text from PyQt4 to UTF-8

查看:28
本文介绍了如何将阿拉伯文本从 PyQt4 转换为 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 a unicode string.

相反,encode("utf-8")的意思是

取一个 unicode 字符串并使用 UTF-8 编码成二进制 str.

take a unicode string and encode into a binary str 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆