Encoding for Multilingual .py Files

2019-03-25 09:12发布

I am writing a .py file that contains strings from multiple charactersets, including English, Spanish, and Russian. For example, I have something like:

string_en = "The quick brown fox jumped over the lazy dog."  
string_es = "El veloz murciélago hindú comía feliz cardillo y kiwi."
string_ru = "В чащах юга жил бы цитрус? Да, но фальшивый экземпляр!"

I am having trouble figuring out how to encode my file to avoid generating syntax errors like the one below when my file is run:

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

I've tried adding # -*- coding: utf-8 -*- to the beginning of my file, but without any luck. I've also tried marking my strings as unicode (i.e. string_en = u'The quick brown fox jumped over the lazy dog."), again unsuccessfully.

Is it possible to include characters from different Python codecs in one file, or am I attempting to do something that is not allowed?

2条回答
我命由我不由天
2楼-- · 2019-03-25 09:19

There are two aspects to proper encoding of strings in your use case:

  1. For Python to understand that you are using UTF-8 encoding, you must include in the first or second line of your code, a line that looks like # coding=utf-8. See PEP 0263 for details.

  2. Your editor also must use UTF-8. This requires to configure it, and depends on the editor you are using. Configuration of Emacs and Vim are addressed in the same PEP, Eclipse can default to the filesystem encoding, which itself can be derived from your locale settings, etc.

查看更多
The star\"
3楼-- · 2019-03-25 09:35

You have to add # -*- coding: XXXX -*- in the beginning of file, replacing the XXXX with the encoding your editor uses to save your source file;

Which editor are you using? Can you check on the editor settings which encoding is being used to save the data?

查看更多
登录 后发表回答