pipeline encoding

Diez B. Roggisch deets at nospam.web.de
Thu Dec 6 14:06:40 EST 2007


Tomek Toczyski schrieb:
> Diez B. Roggisch:
> 
>>
>> Python tries and guesses the stdout-encoding based on the terminal 
>> settings. So the first print works.
>>
>> However, piping to a file means that it can't do so, because it 
>> doesn't (and shouldn't) make any assumptions on the output encoding 
>> desired - after all, it might be appending to a XML-file with e.g. 
>> latin1 encoding.
>>
>> So you need to explictely encode the unicode-object with the desired 
>> encoding:
>>
>>
>> python -c "print u'\u03A9'.encode('utf-8')" > file.txt
> 
> Thanks. It is a solutiona to my problem but:
> 
> Are there any command line option for telling python what encoding to 
> use for stdout?
> 
> To be honest I have a more complicated program than the example that I 
> have presented - there are many print commands inside and it is not very 
> feasible for me to put .encode('utf-8') inside every print occurence.

No it hasn't, and it's easy enough remedied by doing


def eprint(msg):
     print msg.encode('utf-8')

and then doing

eprint('whatever')

instead of

print 'whatever'

Diez



More information about the Python-list mailing list