Skip to content

Commit 6dbd843

Browse files
miss-islingtonWindsooon
authored andcommitted
bpo-36654: Add examples for using tokenize module programmatically (GH-12947)
(cherry picked from commit 4b09dc7) Co-authored-by: Windson yang <[email protected]>
1 parent 548685e commit 6dbd843

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Doc/library/tokenize.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,22 @@ The exact token type names can be displayed using the :option:`-e` option:
267267
4,10-4,11: RPAR ')'
268268
4,11-4,12: NEWLINE '\n'
269269
5,0-5,0: ENDMARKER ''
270+
271+
Example of tokenizing a file programmatically, reading unicode
272+
strings instead of bytes with :func:`generate_tokens`::
273+
274+
import tokenize
275+
276+
with tokenize.open('hello.py') as f:
277+
tokens = tokenize.generate_tokens(f.readline)
278+
for token in tokens:
279+
print(token)
280+
281+
Or reading bytes directly with :func:`.tokenize`::
282+
283+
import tokenize
284+
285+
with open('hello.py', 'rb') as f:
286+
tokens = tokenize.tokenize(f.readline)
287+
for token in tokens:
288+
print(token)

0 commit comments

Comments
 (0)