On this page
  1. tokenize.detect_encoding
  2. Import
  3. Signature & Overview
  4. Usage
  5. Gotchas
  6. REPL Inspect

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

REPL Inspect

from tokenize import detect_encoding
help(detect_encoding)

Linked from

Related notes