Current time: 07-28-2017, 07:56 AM Hello There, Guest! (LoginRegister)

Post Reply 
[SOLVED] Output Encoding of Python3.6 Cron jobs
06-16-2017, 04:23 AM (This post was last modified: 06-16-2017 04:38 AM by Ambious.)
Post: #1
[SOLVED] Output Encoding of Python3.6 Cron jobs
I have a cron job that runs a Python3.6 script with an output that's in Hebrew.
It works great (including output) when I run it through SSH - but when the cron job runs automatically and sends me the output via email, it sends me the error:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 14-19: ordinal not in range(128)

I tried adding: # -*- coding: utf-8 -*- in the header which didn't change anything (nor should it, given that Python3 is UTF-8 by default). Using .encode('utf-8') just turns it into unicode escape characters (b':\xd7\x94\xd7\xa9\xd7\x97\xd7\x9e\xd7\x94\xd7\x9c \xd7\xa1\x etc.), and trying to trick it with .encode('utf-8').decode('utf-8') just gives the same error as before.

Again - what's weird is that through the SSH console, running the exact same command I use for the cron job - the output is fine, which makes this harder to debug (I have to wait 10 minutes after each change to get the email output :-P).
It must be something in the stdout pipe of the cron job that's different than when connecting through SSH, but for the life of me I can't find how to change it.

Any ideas would be appreciated!

Solved by adding
sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())

Now I just have to make my shitty email client read UTF-8 :-D
Find all posts by this user
Quote this message in a reply
Post Reply 

Forum Jump: