Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnicodeEncodeError only when running as a cron job [duplicate]

My program works right in the commandline, but when I run it as a cron job it crashes on the error:

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

It crashes on the statement

print title

Why this is happening only when the app runs as a cron job? How could this be fixed?

I tried (with no help):

print unicode(title)

Python is 2.7

like image 493
xralf Avatar asked Mar 29 '12 19:03

xralf


2 Answers

Nah, you can have it simpler. Just define PYTHONIOENCODING before executing this script. Like this:

PATH=<your path>
MAILTO=someone@somewhere
PYTHONIOENCODING=utf8

* * * * * /run/your/script
like image 197
kworr Avatar answered Oct 31 '22 19:10

kworr


Aside: This is a common problem; as such this is probably a duplicate question.

The default encoding on 2.7 is ascii.
You need to provide an encoding for your program's output.
A common encoding to use is 'utf8'.

So you'd do instead:

print title.encode('utf8')

Here's one way to check the default encoding:

import sys

sys.getdefaultencoding()
# -> 'ascii'
like image 20
mechanical_meat Avatar answered Oct 31 '22 17:10

mechanical_meat