Tokenize / unotkenize leaves unwanted lines
When using tokenize to remove a line from source code untokenize adds back in blank lines for all those lines which I have removed, see example: import tokenize as t from io import StringIO source_code = """a = 'one' b = 'two' c = 'three' d = 'four' e = 'five' """ modi_toks = [] toks = t.generate_tokens(StringIO(source_code).readline) for tok in toks: if not ( 1 < tok.start[0] < 4): modi_toks.append(tok) modi_source = t.untokenize(modi_toks) print(f"{source_code=}") print(modi_source) I have tried writing only (tok.type, tok.string) to the new tokens list but that then causes the white space b
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.