Can ast be used to parse a single line? mode='single' doesn't work on some lines [closed]
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? Example2 below shows how ast.parse(..., mode='single') doesn't appear to work correctly, or at least not as expected. Couldn't find much documentation explaining how it is supposed to work though. import ast line = " x = 1; # test comment" ast_tree = ast.parse(line) modi
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.