0

I have a string of a list of tuples and lists, like so:

"['S', [('I', 'PRP'), (',', ','), ('PERSON', [('Vladamir', 'NNP'), ('Putin', 'NNP')]), (',', ','), ('have', 'VBP'), ('never', 'RB'), ('been', 'VBN'), ('more', 'RBR'), ('frustrated', 'JJ'), ('in', 'IN'), ('my', 'PRP$'), ('life', 'NN'), ('.', '.')]]"

It deals with natural language processing, you can see that each word is given it's corresponding part of speech, and words are also grouped into smaller lists (in this case, "Vladamir Putin").

This value is being returned to me as a string, but I need to turn it back into a list somehow because my goal is converting it into JSON format. I've already tried using the JSON library, the ast library, and turning into a numpy array but JSON and ast return errors while numpy makes a 0 dimensional array (the only element of the array is the string containing the entire list).

Any hints or code-snippets are highly appreciated.

Robbie Barrat
  • 510
  • 1
  • 6
  • 24
  • 1
    *ast return errors*. Really? I tried this and it worked – Moses Koledoye Jul 11 '16 at 16:15
  • !!! Could you please submit an answer with the code you used? I get a "malformed string" error when I try it. – Robbie Barrat Jul 11 '16 at 16:17
  • 1
    Check the [link](http://stackoverflow.com/questions/1894269/convert-string-representation-of-list-to-list-in-python) to the duplicate. It would help – Moses Koledoye Jul 11 '16 at 16:18
  • 1
    Whoops, sorry for submitting a duplicate. I initially tried the answer found on that page before I wrote this question and it didn't work, after fiddling with the format of the string a little bit it works fine. Thank you. – Robbie Barrat Jul 11 '16 at 16:21

0 Answers0