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
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
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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With