I am really lost in all the encoding/decoding issues with Python. Having read quite few docs about how to handle incoming perfectly, i still have issues with few languages, like Korean. Anyhow, here is the what i am doing.
korean_text = korean_text.encode('utf-8', 'ignore')
korean_text = unicode(korean_text, 'utf-8')
I save the above data to database, which goes through fine.
Later when i need to display data, i fetch content from db, and do the following:
korean_text = korean_text.encode( 'utf-8' )
print korean_text
And all i see is '???' echoed on the browser. Can someone please let me know what is the right way to save and display above data.
Thanks
-
should the second 'encode' be a 'decode'?miku– miku2010年01月05日 12:59:12 +00:00Commented Jan 5, 2010 at 12:59
-
Do you have necessary fonts installed?Kugel– Kugel2010年01月05日 13:00:08 +00:00Commented Jan 5, 2010 at 13:00
-
Did you declare your output to be encoded with UTF-8?Gumbo– Gumbo2010年01月05日 13:04:25 +00:00Commented Jan 5, 2010 at 13:04
-
1It's difficult to help you with the information you gave us; e.g. we do not know where korean_text comes from, how the database stores it, etc. etc. Maybe you can try to create a self-contained example. (Perhaps you'll find the solution yourself this way...)oefe– oefe2010年01月05日 13:14:58 +00:00Commented Jan 5, 2010 at 13:14
-
1Your first two lines of code appear to be encoding from unicode to UTF-8 and then decoding it back to unicode -- this is pointless. Where did you get the unicode from in the first place?John Machin– John Machin2010年01月05日 14:12:27 +00:00Commented Jan 5, 2010 at 14:12
3 Answers 3
Even having read some docs, you seem to be confused on how unicode works.
- Unicode is not an encoding. Unicode is the absence of encodings.
utf-8is not unicode.utf-8is an encoding.- You decode utf-8 bytestrings to get unicode. You encode unicode using an encoding, say, utf-8, to get an encoded bytestring.
- Only bytestrings can be saved to disk, database, or sent on a network, or printed on a printer, or screen. Unicode only exists inside your code.
The good practice is to decode everything you get as early as possible, work with it decoded, as unicode, in all your code, and then encode it as late as possible, when the text is ready to leave your program, to screen, database or network.
Now for your problem:
If you have a text that came from the browser, say, from a form, then it is encoded. It is a bytestring. It is not unicode.
You must then decode it to get unicode. Decode it using the encoding the browser used to encode. The correct encoding comes from the browser itself, in the correct HTTP REQUEST header.
Don't use 'ignore' when decoding. Since the browser said which encoding it is using, you shouldn't get any errors. Using 'ignore' means you will hide a bug if there is one.
Perhaps your web framework of choice already does that. I know that django, pylons, werkzeug, cherrypy all do that. In that case you already get unicode.
Now that you have a decoded unicode string, you can encode it using whatever encoding you like to store on the database. utf-8 is a good choice, since it can encode all unicode codepoints.
When you retrieve the data from the database, decode it using the same encoding you used to store it. And then encode it using the encoding you want to use on the page - the one declared in the html meta header <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>. If the encoding is the same used on the previous step, you can skip the decode/reencode since it is already encoded in utf-8.
If you see ??? then the data is being lost on any step above. To know exactly, more information is needed.
7 Comments
Read through this post about handling Unicode in Python.
You basically want to be doing these things:
.encode() text to a particular encoding (such as utf-8) before sending it to the database.
.decode() text back to unicode (from your encoding) when reading it from the database
Comments
The problem is most certainly (especially if other non-ASCII characters appear to work fine) that your browser or OS doesn't have the right fonts to display Korean text, or that the default font used by your browser doesn't support Korean. Try to choose another font until it works.