tokenize — 用于 Python 源代码的 Tokenizer

源代码: Lib/tokenize.py


The tokenize module provides a lexical scanner for Python source code, implemented in Python. The scanner in this module returns comments as tokens as well, making it useful for implementing “pretty-printers”, including colorizers for on-screen displays.

To simplify token stream handling, all operator and delimiter tokens and Ellipsis are returned using the generic OP token type. The exact type can be determined by checking the exact_type property on the 命名元组 返回自 tokenize.tokenize() .

警告

Note that the functions in this module are only designed to parse syntactically valid Python code (code that does not raise when parsed using ast.parse() ). The behavior of the functions in this module is undefined when providing invalid Python code and it can change at any point.

令牌化输入

The primary entry point is a generator :

tokenize. tokenize ( readline )

The tokenize() generator requires one argument, readline , which must be a callable object which provides the same interface as the io.IOBase.readline() method of file objects. Each call to the function should return one line of input as bytes.

The generator produces 5-tuples with these members: the token type; the token string; a 2-tuple (srow, scol) of ints specifying the row and column where the token begins in the source; a 2-tuple (erow, ecol) of ints specifying the row and column where the token ends in the source; and the line on which the token was found. The line passed (the last tuple item) is the physical line. The 5 tuple is returned as a 命名元组 with the field names: type string start end line .

返回的 命名元组 has an additional property named exact_type that contains the exact operator type for OP tokens. For all other token types exact_type equals the named tuple type 字段。

3.1 版改变: Added support for named tuples.

3.3 版改变: 添加支持 exact_type .

tokenize() determines the source encoding of the file by looking for a UTF-8 BOM or encoding cookie, according to PEP 263 .

tokenize. generate_tokens ( readline )

Tokenize a source reading unicode strings instead of bytes.

tokenize() readline argument is a callable returning a single line of input. However, generate_tokens() expects readline to return a str object rather than bytes.

The result is an iterator yielding named tuples, exactly like tokenize() . It does not yield an ENCODING token.

All constants from the token module are also exported from tokenize .

Another function is provided to reverse the tokenization process. This is useful for creating tools that tokenize a script, modify the token stream, and write back the modified script.

tokenize. untokenize ( iterable )

Converts tokens back into Python source code. The iterable must return sequences with at least two elements, the token type and the token string. Any additional sequence elements are ignored.

The reconstructed script is returned as a single string. The result is guaranteed to tokenize back to match the input so that the conversion is lossless and round-trips are assured. The guarantee applies only to the token type and token string as the spacing between tokens (column positions) may change.

It returns bytes, encoded using the ENCODING token, which is the first token sequence output by tokenize() . If there is no encoding token in the input, it returns a str instead.

tokenize() needs to detect the encoding of source files it tokenizes. The function it uses to do this is available:

tokenize. detect_encoding ( readline )

The detect_encoding() function is used to detect the encoding that should be used to decode a Python source file. It requires one argument, readline, in the same way as the tokenize() generator.

It will call readline a maximum of twice, and return the encoding used (as a string) and a list of any lines (not decoded from bytes) it has read in.

It detects the encoding from the presence of a UTF-8 BOM or an encoding cookie as specified in PEP 263 . If both a BOM and a cookie are present, but disagree, a SyntaxError will be raised. Note that if the BOM is found, 'utf-8-sig' will be returned as an encoding.

If no encoding is specified, then the default of 'utf-8' 将被返回。

使用 open() to open Python source files: it uses detect_encoding() to detect the file encoding.

tokenize. open ( filename )

Open a file in read only mode using the encoding detected by detect_encoding() .

Added in version 3.2.