When attempting to encode UTF-8 characters in Python 2 source code, the following error may occur due to the lack of an explicitly declared encoding:
SyntaxError: Non-ASCII character '\xe2' in file bla.py on line 1, but no encoding declared
Python 3:
UTF-8 is the default encoding in Python 3, allowing for the seamless use of Unicode characters anywhere in the source code.
Python 2:
In Python 2, the following header can be included at the beginning of the source file to declare UTF-8 encoding:
# -*- coding: utf-8 -*-
This adheres to the guidelines outlined in PEP 0263. Once declared, UTF-8 can be utilized in strings as follows:
<code class="python"># -*- coding: utf-8 -*- u = 'idzie wąż wąską dróżką' uu = u.decode('utf8') s = uu.encode('cp1250') print(s)</code>
The above is the detailed content of How to Handle UTF-8 Encoding in Python Source Code?. For more information, please follow other related articles on the PHP Chinese website!