Working with utf-8 encoding in Python source [dupl

2018-12-31 18:19发布

问题:

This question already has an answer here:

  • Correct way to define Python source code encoding 6 answers
$ 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?

回答1:

In source header you can declare:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
....

It is described in the PEP 0263:

Then you can use 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)

This declaration is not needed in Python 3 as UTF-8 is the default source encoding (see PEP 3120).

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.



回答2:

Do not forget to verify if your text editor encodes properly your code in utf-8. Otherwise, you may have invisible characters that are not interpreted as utf-8.