Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with UTF-8 encoding in Python source [duplicate]

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 UTF-8 strings in source code?

like image 438
Nullpoet Avatar asked Jun 09 '11 07:06

Nullpoet


People also ask

What does encoding =' UTF-8 do in Python?

UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.

Is UTF-8 encoding for ascii characters is identical to ASCII encoding?

Are the first 128 characters of utf-8 and ascii identical? The first 128 code points are the same, yes.

Why is UTF-8 a good choice for the default editor encoding in Python?

As a content author or developer, you should nowadays always choose the UTF-8 character encoding for your content or data. This Unicode encoding is a good choice because you can use a single character encoding to handle any character you are likely to need. This greatly simplifies things.


Video Answer


1 Answers

In Python 3, UTF-8 is the default source encoding (see PEP 3120), so unicode characters can be used anywhere.

In Python 2, you can declare in the source code header:

# -*- coding: utf-8 -*- .... 

It is described in the PEP 0263:

Then you can use UTF-8 in strings:

# -*- coding: utf-8 -*-  u = 'idzie wąż wąską dróżką' uu = u.decode('utf8') s = uu.encode('cp1250') print(s) 

In addition, it may be worth verifying that your text editor properly encodes your code in UTF-8. Otherwise, you may have invisible characters that are not interpreted as UTF-8.

like image 84
Michał Niklas Avatar answered Sep 28 '22 03:09

Michał Niklas