Discuss / Python / Python supports writing source code in UTF-8 by default

Python supports writing source code in UTF-8 by default

Topic source

RayStorm_

#1 Created at ... [Delete] [Delete and Lock User]

来源:Python官方Unicode指南

Python supports writing source code in UTF-8 by default

同时参考:PEP 0263

To aid with platforms such as Windows, which add Unicode BOM marks to the beginning of Unicode files, the UTF-8 signature '\xef\xbb\xbf' will be interpreted as 'utf-8' encoding as well (even if no magic encoding comment is given).

所以可以认为在UTF-8保存的Python源码并不需要加上# -*- coding: utf-8 -*-声明,会被默认解释成UTF-8,即使在windows平台下使用记事本保存为UTF-8(此时会插入三字节的BOM)也没问题。

不过此时要注意:

If a source file uses both the UTF-8 BOM mark signature and a magic encoding comment, the only allowed encoding for the comment is 'utf-8'. Any other encoding will cause an error.


  • 1

Reply