Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b3a4829

Browse files
committed
show a common usage of detect_encoding
1 parent 0088893 commit b3a4829

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

Doc/library/tokenize.rst

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,17 @@ function it uses to do this is available:
9898
but disagree, a SyntaxError will be raised. Note that if the BOM is found,
9999
``'utf-8-sig'`` will be returned as an encoding.
100100

101-
If no encoding is specified, then the default of ``'utf-8'`` will be returned.
101+
If no encoding is specified, then the default of ``'utf-8'`` will be
102+
returned.
103+
104+
:func:`detect_encoding` is useful for robustly reading Python source files.
105+
A common pattern for this follows::
106+
107+
def read_python_source(file_name):
108+
with open(file_name, "rb") as fp:
109+
encoding = tokenize.detect_encoding(fp.readline)[0]
110+
with open(file_name, "r", encoding=encoding) as fp:
111+
return fp.read()
102112

103113

104114
Example of a script re-writer that transforms float literals into Decimal

0 commit comments

Comments
 (0)