I was wondering what the simplest way is to convert a string representation of a list like the following to a list
:
x = '[ "A","B","C" , " D"]'
Even in cases where the user puts spaces in between the commas, and spaces inside of the quotes, I need to handle that as well and convert it to:
x = ["A", "B", "C", "D"]
I know I can strip spaces with strip()
and split()
and check for non-letter characters. But the code was getting very kludgy. Is there a quick function that I'm not aware of?
Best Solution
ast.literal_eval
: