3

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

1
  • What is the other environment? And is that the actual code that produces the error? Commented Jan 4, 2012 at 1:01

3 Answers 3

2

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.

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

Comments

0

If possible, work with unicodes all the way. If you can't change myfunc, at least convert its result to unicode:

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

If your debug_string is already unicode, it should not be necessary to change anything else. Otherwise decode it as well using the appropriate codec.

Comments

0

Try:

string = myfunc()

debug_string = debug_string.replace("$rep$", string).encode('utf-16')

Or:

string = myfunc()

debug_string = debug_string.replace("$rep$", string).decode('utf-16')

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.