Reputation: 1962
I'm trying to learn about tokens in python. Basically all I know is that tokens are a way for python to recognise different objects of a python program.
I tried playing around as so:
from tokenize import generate_tokens
generate_tokens('hello this is a test string')
I got an error:
<generator object generate_tokens at 0x028D7238>
I expected a series of tuples to be displayed.
Can someone explain the concept of tokens to me and how to generate them in python? What python modules include ways to work with tokens?
Upvotes: 0
Views: 6189
Reputation: 16056
You're making two mistakes:
list()
in order to display the results interactively (programmatic access wants the generator form).file().readline
Fixed code and output in ipython
so it pretty-prints lists better:
In [1]: from tokenize import generate_tokens
In [2]: from cStringIO import StringIO
In [3]: list(generate_tokens(StringIO('hello this is a test string').readline))
Out[3]:
[(1, 'hello', (1, 0), (1, 5), 'hello this is a test string'),
(1, 'this', (1, 6), (1, 10), 'hello this is a test string'),
(1, 'is', (1, 11), (1, 13), 'hello this is a test string'),
(1, 'a', (1, 14), (1, 15), 'hello this is a test string'),
(1, 'test', (1, 16), (1, 20), 'hello this is a test string'),
(1, 'string', (1, 21), (1, 27), 'hello this is a test string'),
(0, '', (2, 0), (2, 0), '')]
For the next level (parsing), use either the standard ast
module or the 3rd-party logilab.astng
package.
Upvotes: 3