user2638731
user2638731

Reputation: 605

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2029'

I am getting this error when I try to convert a QString to a regular python string:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2029' in position 3: ordeal not in range(128)

All I am doing is this:

str(string)

string is the QString but it then gives me that error. How can I fix this?

Upvotes: 1

Views: 1623

Answers (1)

phihag
phihag

Reputation: 288090

The name str in Python 2.x is misleading; for historic reasons, str is bytes - a string of bytes instead of characters. If you try to convert a character string to a bytestring, Python uses ASCII by default. Simply use unicode(string) to get a character string under Python 2.x, or switch to 3.x, where str is actually a character string type.

Upvotes: 1

Related Questions