GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.pythoncentral.io — Python Unicode: Encode and Decode Strings (in Python 2.x) | Python Central This article is on Unicode with Python 2.x If you want to learn about Unicode for Python 3.x, be sure to checkout our Unicode for Python 3.x article. Also, if you're interested in checking if a Unicode string is a number, be sure to checkout our article on how to check if a Unicode string is a number.
Strings are among the most commonly used data types in Python, and there might be times when you want to (or have to) work with strings containing or entirely made up of characters outside of the standard ASCII set (e.g. characters with accents or other markings). Python 2.x provides a data type called a Unicode string for working with Unicode data using string encoding and decoding methods. If you want to learn more about Unicode strings, be sure to checkout Wikipedia's article on Unicode. Note: When executing a Python script that contains Unicode characters, you must put the following line at the top of the script, to tell Python that the code is UTF-8/Unicode formatted. [python] Python Unicode: OverviewIn order to figure out what “encoding” and “decoding” is all about, let’s look at an example string: [python] We can see our string s has a non-ASCII character in it, namely “ü” or “umlaut-u.”. Assuming we’re in the standard Python 2.x interactive mode, let’s see what happens when we reference the string, and when it’s printed: [python] Printing gave us the value that we assigned to the variable, but something obviously happened along the way that turned it from what we typed into the interpreter to something seemingly incomprehensible. The non-ASCII character ü was translated into a code phrase, i.e. “\xfc,“ by a set of rules behind-the-scenes. In other words, it was encoded. At this point, s is an 8-bit string, which to us basically means it isn’t a Unicode string. Let’s examine how to make a Unicode string with the same data. The simplest way is with a “u” prefix in front of the literal string marking it as a Unicode string: [python] If we reference and print [python] We can see that the code phrase for our “umlaut-u” is still “\xfc“ and it prints the same—so does that mean our Unicode string is encoded the same way as our 8-bit string s? To figure that out let’s look at what the [python] Now it seems encoding the Unicode string (with the ‘latin-1’ encoding) retuned the same value as string [python] That’s exactly what it does after all. So then, what difference does it make that Well, the difference is that the Unicode string OK, well... that’s great, but... they were still the same, right? So why does that matter? To illustrate the difference and why it matters, let’s consider a new 8-bit string: [python] Unlike the first one, our new 8-bit string is Why didn’t we just type in (or copy-and-paste) the characters like the last 8-bit string? Well, assuming we’re still using the standard Python 2.x console/IDLE, we couldn’t type or paste this value into the interpreter — as it would not accept the value if we did. Why? Because our The question now is how can we turn these code phrases into the character they’re supposed to display? In the first example using a print statement on [python] Uh-oh... that isn’t right. First of all, that is not an Asian script character. Secondly, it’s more than one character. Simply referencing To create our new Unicode string However, we did get the value of [python] Great, now that we’ve stored the decoded [python] Uh... Isn’t that the same thing we got when we tried to print the Not so fast—there’s one detail that was purposefully glossed-over to prove a point: the encoding we used to decode the string is the same as the first example, the ‘latin-1’ codec. However, the 8-bit string Okay, so there was no way for you really to know that unless explicitly told, but this still illustrates the point: the encoding/codec/rule-set makes all the difference when encoding/decoding strings. With the right encoding, let’s see what happens: [python] FINALLY! Our long-lost script character has been found, and it looks pretty good. Now, try copying-and-pasting the character as input—you’ll find it doesn't work (we’re still talking about Python 2.x interactive mode/IDLE). You might also notice something different about the value of new_u, i.e. it appears to consist of only one code phrase (this time in the form ‘\uXXXX’). This Unicode standard has a unique code phrase for every character or script character that can possibly be displayed on your screen. With that in mind you can also tell that the first time we tried to decode new_s, the value was wrong (“u’\xe5\xad\x97’” has 3 code phrases, and for the Unicode standard, that means 3 unique characters). Well, now that those annoying examples are finished, let’s recap the main points of all this hoopla:
The encoding/codec is like the DNA of your string—even with the same nutrients (input), using the wrong DNA (codec) will give you an orange when you should have had an apple.. |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |