Correct way to define Python source code encoding

后端 未结 6 1789
孤街浪徒
孤街浪徒 2020-11-22 12:15

PEP 263 defines how to declare Python source code encoding.

Normally, the first 2 lines of a Python file should start with:

#!/usr/bin/python
# -*- c         


        
6条回答
  •  忘掉有多难
    2020-11-22 12:45

    PEP 263:

    the first or second line must match the regular expression "coding[:=]\s*([-\w.]+)"

    So, "encoding: UTF-8" matches.

    PEP provides some examples:

    #!/usr/bin/python
    # vim: set fileencoding= :
    

     

    # This Python file uses the following encoding: utf-8
    import os, sys
    

提交回复
热议问题