views:

46

answers:

4

Hi!

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 !

A: 

moving this to the original post

Joe
+3  A: 

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.

DasIch
A: 

Awesome that works like a charm ! Thank you !

Joe
A: 

I suppose that myscript.py has not utf-8 encoding itself so Python interpreter can't recognize the charaters between the quotes. Try just to resave myscript.py in utf-8 encoding.

There is a useful article about using Python with utf-8.

eigenein