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

Popular posts from this blog

android - Inheriting from Theme.AppCompat* -

broadcastreceiver - android BOOT_COMPLETED not received if not activity intent-filter -

basic authentication with http post params android -