Update: ast.literal_eval() results in:
ValueError: malformed node or string: <_ast.Name object at 0x1e20594a8>
Issue
The list I have is stored as a string. The delimiter is a comma, but commas also exists in many of the values. Essentially, I need to...
Turn:
"['John Doe', 'Doe, John','etc']"
Into:
['John Doe','Doe,John','etc']
*On a large scale
Note: There are many lists, each of unique size, with values of unique length. The obvious issue is the comma in 'Doe, John'.
What I tried:
I could .split(', ') and shave around the edges, but I am hoping there is a more simple solution.
I tried to use eval, ast.literal_eval, and ast.parse (Obviously, I have no idea what I'm doing.)
Sample Code:
'''
File contains a list of dictionaries
For each dictionary, one key contains a nested list
Except, the list is actually a string, which is annoying
'''
for dictionary in my_file:
for key,value in dictionary.items():
if key == 'mylist':
print(value)
Output (sample):
['Social Perception', 'Perception, Social', 'Perceptions, Social', 'Social Perceptions']
It looks exactly like a list, but I need it to actually be a list.
Thank you!