Is it possible to use ast to parse a single line so that an ast.Name node can be identified and modified? At the moment this falls over when there is white-space at the beginning of the line. Obviously I could strip this out, modify the ast and then add it back in again but is there a better, more Pythonic' way of doing this? import ast line = " x = 1; # test comment" ast_tree = ast.parse(line) modi_line = ast.unparse(ast_tree) print(modi_line)
Originally asked by NickC on Stack Exchange (stackoverflow) (CC BY-SA 4.0).