1. Technology

How Python Processes Non-English RSS Feeds


5 of 5

Python Encodes Unicode

The encode method is one of Python's built-in functions for string handling. It is not part of the string module. The basic format of the program call is as follows:

 x = y.encode("<encoding>") 
The quotation marks around the encoding are required. In this case, string y has already been assigned and is now being assigned to x in an encoding. The encoding being used may be one of a myriad of encodings that Python can use; for the complete list, see the Python documentation.

In the case of 8-bit Unicode, we want to assign the value of body to output using a "utf-8" encoding. So, rewrite line 105 of the program to read as follows:

 output = body.encode("utf-8") 
Now, when the output is printed in the subsequent line, it will be in Unicode. This also enables the web browser to render it properly.

Other tutorials in this series:

Part 1 | 2 | 3 | 4 | 5
Get the Code!
  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. RSS Reader in Python - Building an RSS Reader With Python - Encoding Unicode in Python

©2014 About.com. All rights reserved.