Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encoding/decoding strings in python

I have a function that returns a utf-16 encoded string and I have to include its result into another string by a replace:

string = myfunc()

debug_string = debug_string.replace("$rep$", string)

In my eclipse environment it works fine, but in another environment it gives an error:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe0' in position 23: ordinal not in range(128)

Do you know what is the possible cause?

Thanks

like image 241
Alessandro Di Maggio Avatar asked Feb 10 '26 16:02

Alessandro Di Maggio


1 Answers

Your string variable isn't in Unicode? Then you need to explicitly decode sequence of bytes (in UTF-16 encoding) from string (string type) to Unicode object:

u_string = myfunc().decode('utf-16')

debug_string also should be in Unicode.

like image 69
demalexx Avatar answered Feb 12 '26 16:02

demalexx



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!