[PyQt] unicode and pyqt4

Wolfgang Rohdewald wolfgang at rohdewald.de
Wed Nov 24 18:29:22 GMT 2010


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


-- 
Wolfgang


More information about the PyQt mailing list