0

At the moment it looks like this:

myString="['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']"

I want it to end up like this:

myArray=['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']

How would I do this?

Theo Hodges
  • 71
  • 1
  • 2
  • 8
  • My first thought was it's a JSON array, but the quotes are swapped around. If they weren't: `import json; myArray=json.loads('["ASK","WHAT","etc"]')` is the best way. – nigel222 May 11 '16 at 09:29

1 Answers1

1
>>> myString="['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']"
>>> list(map(lambda x:x.strip("'"), myString.strip('"[]').split(", ")))
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']

or use this:

>>> import ast
>>> ast.literal_eval(myString)
['ASK', 'NOT', 'WHAT', 'YOUR', 'COUNTRY', 'CAN', 'DO', 'FOR', 'YOU']
riteshtch
  • 8,629
  • 4
  • 25
  • 38