[Character Encoding] UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 [Python]

Tadashi Shigeoka ·  Sun, August 28, 2011

I’ll introduce the solution I found when a UnicodeDecodeError occurred while running a Python program in interactive mode.

Python

Specifically, this is about the UnicodeDecodeError that occurred when I executed a script following the content written on the site below.

When I executed the following program under “Let’s search for books using the Amazon class with the keyword ‘artificial intelligence’” using amazon.py, I got the error below.

xml = amazon.itemSearch("Books", Keywords="人工知能", ItemPage="1")  # Books

Error Content

Traceback (most recent call last):
  File "", line 1, in 
  File "amazon.py", line 44, in itemSearch
    return self.sendRequest(params)
  File "amazon.py", line 76, in sendRequest
    self.url = self.buildURL(params)
  File "amazon.py", line 58, in buildURL
    pair = "%s=%s" % (p[0], urllib2.quote(p[1].encode("utf-8")))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

When I investigated the cause of the error, it was related to character encoding handling.

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

I solved it by reading this. It was educational as it detailed unicode handling.

By changing Keywords to Unicode type as Keywords=u”人工知能” as shown below, it worked normally.

xml = amazon.itemSearch("Books", Keywords=u"人工知能", ItemPage="1")

That’s all.

That’s all from the Gemba.