[PyQt] unicode and pyqt4

Janwillem van Dijk jwevandijk at xs4all.nl
Wed Nov 24 20:52:57 GMT 2010


On 11/24/2010 07:29 PM, Wolfgang Rohdewald wrote:
> On Mittwoch 24 November 2010, Janwillem van Dijk wrote:
>    
>> When I run the script from
>> Eric4 or directly from a terminal with  "python scriptname.py"
>> all is  OK. However I want to run it as a subprocess and than
>> the special chars generate errors:
>>      
> if stdout is pipelined, python tries to encode its output
> to ascii. The following is my solution for python2.6:
>
> from locale import getpreferredencoding
> from sys import stdout
> try:
>      STDOUTENCODING = stdout.encoding
> except AttributeError:
>      STDOUTENCODING = None
> if not STDOUTENCODING:
>      STDOUTENCODING = getpreferredencoding()
>
> def kprint(*args, **kwargs):
>      """a wrapper around print, always encoding unicode to something sensible"""
>      newArgs = [unicode(x).encode(STDOUTENCODING) for x in args]
>      # we need * magic: pylint: disable=W0142
>      print(*newArgs, sep=kwargs.get('sep', ' '), end=kwargs.get('end', '\n'), file=kwargs.get('file'))
>
>
>    
Thanks for tips and solutions.
I added at the top of the script

reload(sys)
sys.setdefaultencoding( 'ISO8859-1' )

and that solves the problem. It now shows German and French accented 
chars correctly both when run from Eric4 and from the console.

I found this solution some time ago when I was having problems with 
processing Cyrillic attachments. It feels a bit like a not too elegant 
brute force solution. Should I make an attempt in the style that Tobias 
suggests??

Cheers, Janwillem


More information about the PyQt mailing list