Encoding for Multilingual .py Files

前端 未结 2 613
礼貌的吻别
礼貌的吻别 2021-02-14 01:20

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         


        
相关标签:
2条回答
  • 2021-02-14 01:59

    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.

    0 讨论(0)
  • 2021-02-14 02:08

    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?

    0 讨论(0)
提交回复
热议问题