-2

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!

Community
  • 1
  • 1

1 Answers1

-3

Original answer

If it looks exactly like a list, then

eval(your_text)

will turn it into the proper list. As simple as that.

Updated

Just saw the answers in the link above (duplicate). import ast and then ast.literal_eval() is indeed the right way to do it.

Israel Unterman
  • 13,158
  • 4
  • 28
  • 35
  • The exception was raised: `Error: name 'fubar' is not defined` I figured out what was wrong, and will post a comment with the solution. Thank you – Asher Sebban Aug 07 '19 at 11:29
  • 1
    Please don't suggest evil `eval` when there are better options: [Eval really is dangerous](https://nedbatchelder.com/blog/201206/eval_really_is_dangerous.html) – Matthias Aug 07 '19 at 11:58
  • @Matthias What are those options? – Asher Sebban Aug 07 '19 at 12:03
  • @AsherSebban: `ast.literal_eval` (slower, but as full featured as you can get without introducing security flaws, parsing any legal Python literal expression) or `json.loads` (faster, but only for JSON, which isn't quite as flexible as Python literals). Your input appears to have unquoted strings though (e.g. `fubar`), so those aren't going to be parsed properly by anything (`eval` might work, but only by loading an existing `fubar` variable, which probably isn't what you want). It sounds like your input may be a JavaScript literal (which allows things JSON doesn't). – ShadowRanger Aug 07 '19 at 13:57