How would I convert a string into a list of tokens in Python? -
when input this:
>>>tokenize('<[2{12.5 6.0}](3 -4 5)>') i want back:
['<', '[', 2, '{', 12.5, 6.0, '}', ']', '(', 3, -4, 5, ')', '>'] basically, how keep input converts list while keeping numbers original value.
you can try use tokenizer, gives same result expected except negative number -4, pretty close.
from stringio import stringio import tokenize str = '<[2{12.5 6.0}](3 -4 5)>' tokens = tokenize.generate_tokens(stringio(str).readline) result = [x[1] x in tokens] here result:
['[', '2', '{', '12.5', '6.0', '}', ']', '(', '3', '-', '4', '5', ')', '>', '']
Comments
Post a Comment