V
V
Vyacheslav2014-09-27 17:43:00
Python
Vyacheslav, 2014-09-27 17:43:00

Running a Python script in cron, how to output Cyrillic?

Greetings.
Input: on the server (Ubuntu), a python script (python 3) is run by cron.
Problem: as soon as Cyrillic comes across (for example: print('Hi')) - the script crashes with an exception like: "UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)".
If you run in the console - everything is ok.
In python 2, this was solved with a small hack:

import sys
reload(sys)                                   
sys.setdefaultencoding('utf-8')

However, setdefaultencoding was removed in the third python
:(

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
overmes, 2014-09-27
@Armann

try setting:
export PYTHONIOENCODING=UTF-8

A
Alexander Karabanov, 2014-09-28
@karabanov

Add the magic comment # -*- coding: utf-8 -*- to the top of your script, like this:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

# some code

And the inscriptions in Cyrillic will start to be displayed adequately.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question