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?
UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.
Are the first 128 characters of utf-8 and ascii identical? The first 128 code points are the same, yes.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With