Working with utf-8 encoding in Python source [dupl

2018-12-31 18:37发布

This question already has an answer here:

$ 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?

2条回答
明月照影归
2楼-- · 2018-12-31 18:52

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.

查看更多
无色无味的生活
3楼-- · 2018-12-31 19:09

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.

查看更多
登录 后发表回答