When to use utf8 as a header in py files

后端 未结 5 1616
误落风尘
误落风尘 2021-02-05 05:32

Some source files, from downloaded code, have the following header

# -*- coding: utf-8 -*-

I have an idea what utf-8 encoding is but why would

5条回答
  •  悲哀的现实
    2021-02-05 05:37

    Always use UTF-8 and make sure your editor also uses UTF-8. Start your Python script like this if you use Python 27:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    from __future__ import unicode_literals
    

    This is a good blog post from Nick Johnson about Python and UTF-8:

    http://blog.notdot.net/2010/07/Getting-unicode-right-in-Python By the way, this post was written before he could use:

    from __future__ import unicode_literals
    

提交回复
热议问题