0

I have a Python v3.6 script which reads from database which has some data with Chinese characters. This script gives error when I schedule it to run from Mac Cron Job; however executes fine when run from my Mac's Terminal.

Any idea, if we need to enable some language pack or something when we run script from Cron job?

Crontab command:

#*/2 * * * * /usr/local/bin/python3 /Users/admin/Documents/Scripts/wht/gbi.py >> test.txt
4
  • Update the question with the error Commented Oct 31, 2017 at 10:35
  • Maybe it is using python 2.x rather than 3.6 Commented Oct 31, 2017 at 10:36
  • I have given the path till python 3 in my cron job command as well Commented Oct 31, 2017 at 10:39
  • Can you post the errors thrown when run as cron Commented Oct 31, 2017 at 10:41

1 Answer 1

1

Let's encode those characters. Example:

DON'T use them directly in source code:

 SPECIAL_CHARS = u"aAàÀảẢãÃáÁạẠăĂằẰẳẲẵẴắẮặẶâÂầẦẩẨẫẪấẤậẬ"

Let's use:

 SPECIAL_CHARS = u'aA\xe0\xc0\u1ea3\u1ea2\xe3\xc3\xe1\xc1\u1ea1\u1ea0\u0103\u0102\u1eb1\u1eb0\u1eb3\u1eb2\u1eb5\u1eb4\u1eaf\u1eae\u1eb7\u1eb6\xe2\xc2\u1ea7\u1ea6\u1ea9\u1ea8\u1eab\u1eaa\u1ea5\u1ea4\u1ead\u1eac'

You can encode string to utf-8 and use it normally:

 encoded_str = SPECIAL_CHARS.encode('utf-8')

 print(type(encoded_str))
 print(encoded_str)
Sign up to request clarification or add additional context in comments.

1 Comment

Oh, I get those from database and don't know before hand what all characters will be there. Is there any API available for doing this? [I am new to Python :)]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.