0

I have built a Django management command that prints out some stuff on stdout (using print). All works fine, but...

When I try to redirect the output to a file, using

./manage.py my_command > a_file.txt

I get:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u2013' in position 14: ordinal not in range(128)

No traceback, just that.

I tried ./manage.py my_command | less just for the fun of it, and it showed some output, presumably lines before first non-ASCII char was encountered. Output has some UTF-8 chars, no way around that.

Same error happens on Mac laptop and on Red Hat Linux server. Using Python 2.7.9

What's happening here, and how can I dump the info to a file?

frnhr
  • 12,354
  • 9
  • 63
  • 90
  • Sorry, I was too quick with my previous comment. But I think this [SO answer](http://stackoverflow.com/a/4546129/870769) could be of help. It suggests configuring the `PYTHONIOENCODING` env variable when redirecting or piping. Maybe it helps. – sthzg Apr 20 '15 at 18:09
  • @sthzg Oh yeah! interstingly enough, `print sys.stdout.encoding` showed `UTF-8`, but after defining `$ export PYTHONIOENCODING=UTF-8` the problem is no more. Go figure... – frnhr Apr 20 '15 at 18:17
  • possible duplicate of [UnicodeDecodeError when redirecting to file](http://stackoverflow.com/questions/4545661/unicodedecodeerror-when-redirecting-to-file) – frnhr Apr 20 '15 at 18:18

1 Answers1

1

You can change the standard output of the script with UTF-8 encoded output :

import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)

Or you can encode your strings before printing them:

print string_containing_unicode.encode('utf-8')