Skip to content

bpo-36654: add example to generate token from another file #12947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 25, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Doc/library/tokenize.rst
Original file line number Diff line number Diff line change
Expand Up @@ -278,3 +278,15 @@ The exact token type names can be displayed using the :option:`-e` option:
4,10-4,11: RPAR ')'
4,11-4,12: NEWLINE '\n'
5,0-5,0: ENDMARKER ''

Example of tokenizing a file programmatically::

import tokenize
f = open('hello.py', 'rb')
token_gen = tokenize.tokenize(f.readline)

for token in token_gen:
# TokenInfo(type=59 (ENCODING), string='utf-8', start=(0, 0), end=(0, 0), line='')
# TokenInfo(type=1 (NAME), string='def', start=(1, 0), end=(1, 3), line='def foo:\n')
# ...
print(token)