2015年5月28日 星期四

[ Python 文章收集 ] Understanding Python unicode, str, UnicodeEncodeError and UnicodeDecodeError

Source From Here 
Agenda 
* Understanding the basics of Python 'unicode' and 'str' type
* Deliberately causing UnicodeEncodeError and UnicodeDecodeError and fixing it.
* A practical example showing how encoding issues can trip you.

To follow along easily, it would help if you understand concept of unicode, encoding and decoding in general. Please refer to our last blog to understand the basics of unicode and encoding. This post assumes you use Python 2.7 and this will not be useful if you are using Python 3. 

Basics 
Make sure your terminal encoding is set to utf-8. 

As discussed in last post, Unicode is just a standard which gives codepoint for different characters. You cannot store codepoint of a character on disk. Codepoint of the character must be encoded using some encoding scheme before it can be stored in a file. 

Codepoints are integers. eg: Codepoint of character 'a' is U+0061 which is integer 97. This codepoint has a different binary representation in different encoding schemes. Or other way of saying it is, this codepoint has different byte sequence in different encoding schemes. And the byte sequence gets written to disk when we write 'a' to a file. 

Codepoint of 'ä' is U+00E4, which is integer 228. This codepoint has a different binary representation, or byte sequence, in different encoding schemes. 

Usually binary representation will not be shown to you. The binary representation would be converted to a hexadecimal number in the output. eg: In 'utf-8' encoding, 'ä' is represented by '11000011 10100100'. But most of the times you will see it's hexadecimal equivalent which is 'c3a4', written as '\xc3\xa4'. 

Python has two different datatypes. One is 'unicode' and other is 'str'. Type 'unicode' is meant for working with codepoints of characters. Type 'str' is meant for working with encoded binary representation of characters

A 'unicode' object needs to be converted to 'str' object before Python can write the character to a file. A 'unicode' object needs to be converted to 'str' object for the character to be printed. 

Python 'unicode' and 'str' type 
We will use a character which has different binary representation in different encoding schemes. ä is one such character. This character is called 'LATIN SMALL LETTER A WITH DIAERESIS'. Codepoint for this character is U+00E4. You can check it at http://www.utf8-chartable.de/ 

The way to define a Unicode codepoint is: 
>>> uni_latin_a = u'\u00e4'
>>> type(uni_latin_a)
unicode
'>
A unicode starts with 'u' followed by quote and the codepoint has to be preceded by '\u'. Let's define a 'str': 
>>> str_normal_a = 'a'
>>> type(str_normal_a)

UnicodeEncodeError 
Let's try to convert 'unicode' to 'str': 
>>> str_latin_a = uni_latin_a.encode()
Traceback (most recent call last):
File "", line 1, in

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128)

When 'encode()' is called, by default ascii encoding scheme is used. So 'encode()' is equivalent to 'encode('ascii')'. ascii can only encode characters whose codepoint is less than 128. uni_latin_a represents a character whose codepoint is greater than 128. And so we get a UnicodeEncodeError

utf-8 encoding scheme can encode codepoints greater than 128. Let's use 'utf-8' to encode uni_latin_a
# "encode()" is meant to be used on a 'unicode' to get a 'str'.
# Make sure you are call 'encode()' on a 'unicode' and never call it on a 'str'.

>>> str_latin_a = uni_latin_a.encode('utf-8')
>>> type(str_latin_a)

# Let's see the hexadecimal representation of str_latin_a.
>>> str_latin_a
'\xc3\xa4'
>>> print(str_latin_a)
ä

So, utf-8 representation of codepoint 'U+00E4' is '\xc3\xa4'. You can also verify it at the table provided at http://www.utf8-chartable.de/. 

A 'unicode' cannot be written to a file. 
>>> f = open('uni_latin_a.txt', 'w')
>>> f.write(uni_latin_a)
Traceback (most recent call last):
File "", line 1, in
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 0: ordinal not in range(128)

A 'unicode' object must be encoded to get it's binary representation, and then encoded binary representation gets written to the file. 

Python is trying to do implicit encoding here. Python can only write 'str' to a file. Since we are passing a 'unicode' to write, python tries to convert the 'unicode' into 'str'. Internally Python runs f.write(uni_latin_a.encode('ascii')) which cause a UnicodeEncodeError

Encode uni_latin_a using utf-8 so it can be written: 
>>> f.write(uni_latin_a.encode('utf-8'))
>>> f.close()
# Check uni_latin_a.txt in your editor. If your editor understands utf-8 encoded strings, it will show the expected character.


UnicodeDecodeError 
UnicodeDecodeError will usually happen when you try to process something read from a file. 

We just wrote a utf-8 encoded character to 'uni_latin_a.txt'. Let's read this file. 
>>> f = open('uni_latin_a.txt')
>>> read_str_latin_a = f.read()
>>> f.close()
>>> print read_str_latin_a
ä
# 'type()' verifies that whenever we read from a file, we get an instance of 'str', and not an instance of 'unicode'.
>>> type(read_str_latin_a)

# Let's see the hexadecimal representation of read_str_latin_a
>>> read_str_latin_a
'\xc3\xa4'

Several times it makes sense to work with Unicode internally and in such case we will need to convert the read value into unicode. Decoding is the process of converting an encoded representation into Unicode codepoint

'.decode()' is meant to convert from 'str' to 'unicode'. Always use '.decode()' on a 'str'. Never use it on 'unicode' object. Let's try converting read_str_latin_a to a 'unicode' object: 
>>> read_uni_latin_a = read_str_latin_a.decode()
Traceback (most recent call last):
File "", line 1, in

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 0: ordinal not in range(128)

When '.decode()' is called, Python default thinks that the string was encoded using 'ascii'. So it tries to find the Unicode codepoint which corresponds to this encoded representation. In ascii, no Unicode codepoint corrresponds to '\xc3\xa4' and so an error is raised

We already know that encoding was done using 'utf-8' when writing to the file. So use 'utf-8' with decode(). 
>>> read_uni_latin_a = read_str_latin_a.decode('utf-8')
>>> print read_uni_latin_a
ä

Suppose we did not know that the file content was encoded with 'utf-8'. In that case, we could have tried decoding it with latin-5 or any other encoding scheme. Suppose we try latin-5: 
>>> read_uni_latin_a = read_str_latin_a.decode('8859')
>>> print read_uni_latin_a
ä

In encoding scheme 8859, U+00C3 when encoded gives hexadecimal '\xc3' and U+00A4 when encoded gives hexadecimal '\xa4'. So when '\xc3\xa4' is decoded, it gives back codepoints U+00C3 and U+00A4. Codepoint U+00C3 means 'Ã' and codepoint U+00A4 means '¤'. And that's what we see in output. 

That's why it's important to know the encoding of a file otherwise we will read it wrong. 

Takeaway 
* Unicode codepoints can be stored in type 'unicode'. When unicode codepoints are defined, they are stored in 'unicode' and haven't been converted to a particular encoding.
* Unicodes are prepended with u''. When we do this, python understands that we want to store the codepoint of the character/string.
* Make sure you .encode() a unicode and not a string.
* Make sure you .decode() a string and not a unicode.

Supplement 
Unicode HOWTO

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...