tokenize.detect_encoding
Up: tokenize | Next: tokenize.generate_tokens
Rel: tokenize.generate_tokens, tokenize.open, tokenize.TokenError, tokenize.TokenInfo
Ref: docs.python.org — tokenize.detect_encoding
Tags: #public #python3 #stdlib #tokenize
Import
import tokenize
from tokenize import detect_encoding
Signature & Overview
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.
Usage
import io, tokenize
enc, lines = tokenize.detect_encoding(io.BytesIO(b'# -*- coding: latin-1 -*-\nx = 1\n').readline)
assert enc == 'iso-8859-1'
Gotchas
- Implements PEP 263 (
# coding: ...+ UTF-8 BOM detection, defaulting to'utf-8').
REPL Inspect
from tokenize import detect_encoding
help(detect_encoding)
Linked from
- tuts/python3/stdlib_module/tokenize
- tuts/python3/stdlib_module/tokenize/generate_tokens
- tuts/python3/stdlib_module/tokenize/open
- tuts/python3/stdlib_module/tokenize/TokenError
- tuts/python3/stdlib_module/tokenize/TokenInfo
- tuts/python3/stdlib_module/tokenize/tokenize
- tuts/python3/stdlib_module/tokenize/untokenize
Related notes
- tuts/python3/stdlib_module/tokenize/generate_tokens Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/open Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/TokenError Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/TokenInfo Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/tokenize Linked here; links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize/untokenize links to this page; shared tags: #stdlib, #tokenize
- tuts/python3/stdlib_module/tokenize links to this page; shared tags: #stdlib
- tuts/c/stdlib shared tags: #stdlib
- tuts/c/stdlib/assert shared tags: #stdlib
- tuts/c/stdlib/assert/assert shared tags: #stdlib