質問

I try to print a PyQt4.QtCore.QString object:

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

However, I get the following error:

<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)

What is going wrong here?

役に立ちましたか?

解決

It is possible with

print unicode(html)

他のヒント

In python 2.x str returns an ascii string. So if there is a unicode character in your input, it can not be converted. The unicode function serves you instead then.

If you use for example python 3.3, str function would have an encoding parameter which is utf8 by default. Therefore you won't see the error.

Look here for more fetails.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top