4

I am working with a database that has throughout it scattered characters like this: â€TM. I need to take this from the database, convert it to UTF-8, and then import it into a different database, using python. When printed to the Windows Command Prompt, these characters look like this: \xe2\u20ac\u2122. I have tried various combinations of .decode(), .encode(), and unicode() to convert the data, but Im really stuck.

asked Jul 19, 2011 at 0:02
6
  • Well, what is the database encoding? UTF-16? Commented Jul 19, 2011 at 0:03
  • The database encoding is UTF_8_bin Commented Jul 19, 2011 at 0:09
  • Did you try blah.decode('utf-8-sig')? What happens when you do? Commented Jul 19, 2011 at 0:27
  • 3
    The Windows command-prompt cannot display UTF-8 as it uses Win-1252 (a variant of ISO-8859-1). What you see is the result of trying to display UTF-8 in a non-UTF-8 environment. Why is the display of the data in a command-prompt important? Commented Jul 19, 2011 at 0:29
  • To display properly at the prompt you might want to try putty and connect to localhost, putty supports UTF-8 under the Windows->Translation option. Although I've personally had little luck with this feature. Commented Jul 19, 2011 at 1:02

1 Answer 1

10

Always decode on input, and encode on output. (There ought to be handy mnemonic for this: perhaps "take your code [coat] off when you come indoors".)

Decode on input: You say that the database encoding is "UTF_8_bin". Are you using MySQL-Python? If so, then you can set the use_unicode option when you connect to the database. Then all strings are fetched from the database in Unicode, so you don't have worry about decoding them.

Encode on output: You can find out the current character encoding (or "code page" as they call it in Windows) with the chcp command. Let's suppose it's code page 1252. Then you can write

print text.encode('windows-1252')

to produce something that you can read from the Windows command line.

If you're writing the strings back to another MySQL database using MySQL-Python, you shouldn't need to do anything special: MySQL-Python claims that "you can always write Unicode strings" (regardless of whether you specified use_unicode when you opened the connection).

answered Jul 19, 2011 at 1:48
Sign up to request clarification or add additional context in comments.

3 Comments

Where exactly would I specify the use_unicode option? I am running a standalone script on a django app; Do I add this to the settings.py file, or somewhere else?
It's an option to MySQLdb.connect. I don't know anything about Django, but the documentation suggests that you could put it under DATABASES.default.OPTIONS in your configuration.
DIEO (read as "die-oh")? Despise incompetent, evil offenders? (I'm not very creative. lol)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.