Let’s say I have a string in the following form:
myString={"name", "age", "address", "contacts", "Email"}
I need to get all the items of myString into a list using python. Here’s what I did:
r = re.search("myString=\{\"(.+)\", $\}", line)
if r:
items.append(r.group(1))
print(items)
Here line is the variable that holds the content of my text file.
What change do I have to make to my regex to get all the items in myString?
Looks like valid set notation so you could use the ast module to parse it instead:
EDIT: In light of the actual format of the input file, I would use a regex to parse out the block and then parse the block as above, or as bellow to just strip off the quotes:
But probably the best way is to combine the two: