Working with UTF-8 encoding in Python source

819    Asked by EmilyColeman in Python , Asked on Apr 13, 2021

Consider:

$ cat bla.py

u = unicode('d…')

s = u.encode('utf-8')

print s

$ python bla.py

File "bla.py", line 1

SyntaxError: Non-ASCII character 'xe2' in file bla.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can I declare python coding UTF-8 strings in the source code?

Answered by Emily Coleman

If you are working with Python 3 this declaration is not needed as UTF-8 is the default source encoding. One important point to note here, you should verify that your text editor properly encodes your code in UTF-8. Otherwise, you may have invisible characters that are not interpreted as UTF-8.


Before writing the code you should declare the source header at the start.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
Below is the code that illustrates the use of python coding utf 8 in strings:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
u = 'idzie wąż wąską dróżką'
uu = u.decode('utf8')
s = uu.encode('cp1250')
print(s)

Your Answer

Interviews

Parent Categories