“编码=utf8”和“-*-编码:utf-8 -*-”有什么区别?

时间:2023-01-06 08:18:18

Is there any difference between using

使用之间有什么区别吗?

#coding=utf8

and

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

What about

是什么

# encoding: utf-8

1 个解决方案

#1


21  

There is no difference; Python recognizes all 3. It looks for the pattern:

没有区别;Python承认所有3。它寻找模式:

coding[:=]\s*([-\w.]+)

on the first two lines of the file (which also must start with a #).

在文件的前两行(也必须以#开头)。

That's the literal text 'coding', followed by either a colon or an equals sign, followed by optional whitespace. Any word, dash or dot characters following that pattern are read as the codec.

这是文本“编码”,后面跟着冒号或等号,后面跟着可选的空格。在此模式下的任何单词、破折号或圆点字符都被视为编解码器。

The -*- is an Emacs-specific syntax; letting the text editor know what encoding to use. It makes the comment useful to two tools. VIM supports similar syntax.

-*是一种特定于emac的语法;让文本编辑器知道要使用什么编码。它使注释对两个工具有用。VIM支持类似的语法。

See PEP 263: Defining Python Source Code Encodings.

参见PEP 263:定义Python源代码编码。

#1


21  

There is no difference; Python recognizes all 3. It looks for the pattern:

没有区别;Python承认所有3。它寻找模式:

coding[:=]\s*([-\w.]+)

on the first two lines of the file (which also must start with a #).

在文件的前两行(也必须以#开头)。

That's the literal text 'coding', followed by either a colon or an equals sign, followed by optional whitespace. Any word, dash or dot characters following that pattern are read as the codec.

这是文本“编码”,后面跟着冒号或等号,后面跟着可选的空格。在此模式下的任何单词、破折号或圆点字符都被视为编解码器。

The -*- is an Emacs-specific syntax; letting the text editor know what encoding to use. It makes the comment useful to two tools. VIM supports similar syntax.

-*是一种特定于emac的语法;让文本编辑器知道要使用什么编码。它使注释对两个工具有用。VIM支持类似的语法。

See PEP 263: Defining Python Source Code Encodings.

参见PEP 263:定义Python源代码编码。