这是从哪里来的:-*- 编码:utf-8 -*- [英] Where does this come from: -*- coding: utf-8 -*-
问题描述
Python 将以下内容识别为定义文件编码的指令:
Python recognizes the following as instruction which defines file's encoding:
# -*- coding: utf-8 -*-
我之前肯定看到过这种指令(-*- var: value -*-
).它从何而来?什么是完整规格,例如值可以包括空格、特殊符号、换行符,甚至 -*-
本身吗?
I definitely saw this kind of instructions before (-*- var: value -*-
). Where does it come from? What is the full specification, e.g. can the value include spaces, special symbols, newlines, even -*-
itself?
我的程序将编写纯文本文件,我想使用这种格式在其中包含一些元数据.
My program will be writing plain text files and I'd like to include some metadata in them using this format.
推荐答案
这种指定 Python 文件编码的方式来自 PEP 0263 - 定义 Python 源代码编码.
This way of specifying the encoding of a Python file comes from PEP 0263 - Defining Python Source Code Encodings.
它也被 GNU Emacs 识别(参见 Python 语言参考,2.1.4 编码声明),虽然我不知道它是否是第一个使用该语法的程序.
It is also recognized by GNU Emacs (see Python Language Reference, 2.1.4 Encoding declarations), though I don't know if it was the first program to use that syntax.
这篇关于这是从哪里来的:-*- 编码:utf-8 -*-的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!