Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
parsing Python-like code
#4
Maybe use the ast module?

>>> import ast
>>> ast.parse('thing[4] = "banana"')
<_ast.Module object at 0x000002619ECBB1D0>
>>> pp(_)
ast.Module(
    body=[
        ast.Assign(
            targets=[
                ast.Subscript(
                    value=ast.Name(id='thing', ctx=ast.Load()),
                    slice=ast.Index(value=ast.Num(n=4)),
                    ctx=ast.Store()
                )
            ],
            value=ast.Str(s='banana')
        )
    ]
)
Reply


Messages In This Thread
parsing Python-like code - by Skaperen - Nov-04-2020, 07:40 PM
RE: parsing Python-like code - by ndc85430 - Nov-05-2020, 05:13 AM
RE: parsing Python-like code - by Skaperen - Nov-05-2020, 05:31 AM
RE: parsing Python-like code - by stranac - Nov-05-2020, 07:49 AM
RE: parsing Python-like code - by Skaperen - Nov-06-2020, 12:17 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  An article of GvR about the parsing of python Gribouillis 0 2,360 Jan-18-2020, 11:28 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020