【问题标题】:Python encoding error when trying to print a QString尝试打印 QString 时出现 Python 编码错误
【发布时间】:2014-02-02 13:14:00
【问题描述】:

我尝试打印一个 PyQt4.QtCore.QString 对象:

print str(type(html))
print str(html)

但是,我收到以下错误:

<class 'PyQt4.QtCore.QString'>
Traceback (most recent call last):
  File "download.py", line 23, in <module>
    print str(html)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2039' in position 4165: ordinal not in range(128)

这里出了什么问题?

【问题讨论】:

    标签: python qstring python-unicode


    【解决方案1】:

    有可能

    print unicode(html)
    

    【讨论】:

      【解决方案2】:

      在 python 2.x 中,str 返回一个 ascii 字符串。因此,如果您的输入中有 unicode 字符,则无法转换。然后 unicode 函数为您服务。

      如果您使用例如 python 3.3,str 函数将有一个 encoding 参数,默认为 utf8。因此您不会看到错误。

      查看here 了解更多详情。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-07-22
        • 2021-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-20
        • 1970-01-01
        相关资源
        最近更新 更多