0

I have a small webapp that runs Python on the server side and javascript (jQuery) on the client side. Now upon a certain request my Python script returns a unicode string and the client is supposed to put that string inside a div in the browser. However i get a unicode encode error from Python.

If i run the script from the shell (bash on debian linux) teh script runs fine and prints the unicode string.

Any ideas ?

Thanks!

EDIT

This is the print statement that causes the error:

print u'öäü°'

This is the error message i get: UnicodeEncodeError: 'ascii' codec can't encode characters in position 34-36: ordinal not in range(128)

However i only get that message when calling the script via ajax ( $('#somediv').load('myscript.py'); )

Thank you !

0

1 Answer 1

3

If the python interpreter can't determine the encoding of sys.stdout ascii is used as a fallback however the characters in the string are not part of ascii, therefore a UnicodeEncodeError exception is raised.

A solution would be to encode the string yourself using something like .encode(sys.stdout.encoding or "utf-8"). This way utf-8 is used as a fallback instead of ascii.

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

Comments

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.