11

Python UTF-8 print fails when redirecting stdout

 3 years ago
source link: https://blog.mathieu-leplatre.info/python-utf-8-print-fails-when-redirecting-stdout.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Python UTF-8 print fails when redirecting stdout

Python UTF-8 print fails when redirecting stdout

Wed 26 January 2011

Consider the following piece of code:

# -*- coding: utf-8 -*-
print u"Վարդանաշեն"

Running this in a terminal works:

$ python test.py
Վարդանաշեն

Redirecting standard output to a file fails:

$ python test.py > file
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    print u"Վարդանաշեն"
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-9: ordinal not in range(128)

Explanations are available on Python official wiki: default encoding has to be forced.

With an environment variable:

$ PYTHONIOENCODING='utf_8'
$ export PYTHONIOENCODING
$ python test.py > file
$

With source modification:

import sys
import codecs
import locale
sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout)

#python, #encoding, #shell - Posted in the Dev category


© Copyright 2020 by Mathieu Leplatre. mnmlist Theme

Content licensed under the Creative Commons attribution-noncommercial-sharealike License.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK