[PyQt] UTF-8 files handling in PyQT4

Henrik Pauli ralesk at muszaki.info
Sat May 5 16:40:35 BST 2007


On 2007. May 5., piotr maliński wrote:
> That doesn't help. It won't even open a file:
>
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xc4 in position
> 14: ordinal not in range(128)
>
> It wants to use ASCII "codec". I've noticed that for example
> QTextStream has "setCodec" but there isn't anything about encoding for
> QFile or textEdit.
> 

If you get back a QString or something similar, you convert it to unicode by 
embracing it like this: unicode(myqstringlikevar).  Then you have to make a 
byte stream out of it (which str is), by using the encode method.  So try: 
unicode(myqstringlikevar).encode("utf8") and see if it works.



More information about the PyQt mailing list