1

I have a python script that retrieves information from a web service and then looks up data in a MySQL db. The data is unicode when I receive it, however I want the SQL statement to use the actual character (Băcioi in the example below). As you can see, when I try and encode it to utf-8 the result is still not what I'm looking for.

>>> x = u'B\u0103cioi'
>>> x
u'B\u0103cioi'
>>> x.encode('utf-8')
'B\xc4\x83cioi'
>>> print x
Băcioi ## << What I want!

1 Answer 1

4

Your encoding is working fine. Python is simply showing you the repr()'d version of it on the command line, which uses \x escapes. You can tell because of the fact that it's also displaying the quotes around the string.

print does not do any mutation of the string - if it prints out the character you want, that's what is actually in the contents of the string.

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

2 Comments

@MДΓΓБДLL - what do you mean? They're already getting it to print that way, because that's what the string is. No changes are necessary.
Yep, that was a dumb comment. I'll be over in the corner

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.