【问题标题】:Python Qt QLineEdit weird encodingPython Qt QLineEdit 奇怪的编码
【发布时间】:2014-08-03 11:47:44
【问题描述】:

我遇到了一个让我很困惑的情况。我的界面中有一个 QLineEdit,所以当我用以下文本填充它时 ???????????? ??????????' ?????? ????????????????????? ?????? (它是阿拉姆语)并且我在我的机器上安装了正确的字体,所以我可以在浏览器中看到字体,除了这些字体我必须自己安装,所以你可能会看到一些奇怪的字符。

self.editor = QtGui.QLineEdit(self)
self.editor.setText(???????????? ????????' ???????? ???????????????????? ????????)

所以在 ui 的编辑器字段中,我可以正确看到文本,但是当我尝试重新提取该文本时,我得到了一些奇怪的东西:

editor_text = self.editor.text()

这里有一个例外:

print(editor_text)
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud802' in position 0:     
surrogates not allowed

pprint(editor_text)
"\ud802\udc40\ud802\udc4c\ud802\udc53 \ud802\udc4a\ud802\udc53' "
'\ud802\udc53\ud802\udc41 '
'\ud802\udc40\ud802\udc41\ud802\udc53\ud802\udc44\ud802\udc4c '
'\ud802\udc4f\ud802\udc4b'

我试过了:

editor_text = self.editor.text().encode(encoding='utf-8', errors='surrogateescape')
UnicodeEncodeError: 'utf-8' codec can't encode character '\ud802' in position 0:    
surrogates not allowed

我该怎么做才能解决这个问题,谢谢

编辑:

我当然添加了标题

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

【问题讨论】:

    标签: python encoding pyqt qlineedit


    【解决方案1】:
    1. 将此添加到源标题:

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

      您可以阅读此主题:PEP Index

    2. 在您的阿拉姆语字符串中,将“u”添加到上述 Python 字符串中,以便在 unicode 模式下使用,如下所示:

      u' ܐܪܡܝܐ‎ '
      

      u'Unicode String'
      

    完成的代码:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtCore, QtGui
    class exampleQMainWindow (QtGui.QMainWindow):
        def __init__ (self):
            super(exampleQMainWindow, self).__init__()
            testQLineEdit = QtGui.QLineEdit(self)
            testQLineEdit.setText(u'ܐܪܡܝܐ‎')
            print testQLineEdit.text()
            self.setCentralWidget(testQLineEdit)
    
    app = QtGui.QApplication([])
    window = exampleQMainWindow()
    window.show()
    sys.exit(app.exec_())
    

    您可以像这样从其他地方获取变量:

    text = u'ܐܪܡܝܐ‎'
    testQLineEdit = QtGui.QLineEdit(self)
    testQLineEdit.setText(text)
    print testQLineEdit.text()
    

    要强制字符串为 Unicode,您可以使用unicode(String)

    这是返回文本的输出测试类型:

    >> print type(unicode(testQLineEdit.text()))
    <type 'unicode'>
    
    >> print type(testQLineEdit.text())
    <class 'PyQt4.QtCore.QString'>
    
    >> print type(testQLineEdit.text().toUtf8())
    <class 'PyQt4.QtCore.QByteArray'>
    

    所有条件都可以在控制台打印。

    【讨论】:

    • 谢谢我已经有了源标题,但是对于你的回答 2,如果我有 text='REXTENSE.setText(utext) u text 给出错误,我不能直接输入字符串正如你所说,因为它来自其他地方的变量
    • 请像这样在声明变量中使用'u';文本 = u' testQLineEdit.setText(utext);
    • 对不起,我表达错了,我实际上并没有声明变量 var=u'RR支RR‎' 我从其他方法 var=testQLineEdit.text() 获取文本。字符串创建后如何添加 u ?类似 var2=u+var 或 var2=u+str(var)
    • 实际上,如果您添加源头编码:utf-8 它可以读取您的阿拉姆字符。如果无法读取,您可以像这样强制使用 Unicode 字符串中的字符串; var=unicode(testQLineEdit.text());
    • 非常感谢,但不幸的是它不起作用,在 QLineEdit 中填充字符串之前,字符串打印良好,在填充 QLineEdit 并从 QLineEdit 中提取它之后,字符串在翻译中丢失了。 QLineEdit 也不尊重文本方向(从右到左)
    【解决方案2】:

    我所要做的就是

    print unicode(testQLineEdit.text())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 1970-01-01
      • 2017-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      相关资源
      最近更新 更多