0

A system call exists in PHP such that

system("python3 returnUTF8.py")

If I run the Python script on the Linux command line - it works fine, however when I use the PHP system command I get an error "UnicodeEncodingError - can't encode to ascii"...

I understand the problem to be that the print function in Python defaults to the result the caller wants, and this is shown when I run print (sys.stdout.encoding) on the command line I get UTF-8 and from the system call I get ANSI_X3.4-1960.

How can I change the system call in PHP or the Python script to use UTF-8 encoding?

1 Answer 1

3

You'll need to tell Python what encoding to use; normally this is taken from your terminal locale, but when you use system() there is no such locale and then the default C locale encoding is used, which is ANSI_X3.4-1960 (aka ASCII).

Set the PYTHONIOENCODING environment variable:

system("PYTHONIOENCODING=utf-8 python3 returnUTF8.py")

or explicitly encode to UTF-8 in your program.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much! That's brought a conclusion to 2 hours of frustration. :)

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.