-1

I have used named entity recognition using spacy on a newspaper report and added the output in a pandas dataframe column. This is what I've got:

[('Madaripur', 'GPE'), ('Sadar', 'GPE'), ('Madaripur', 'GPE'),
 ('Wednesday', 'DATE'), ('Rozina Parveen', 'PERSON'), ('55', 'DATE'),
 ('Jalal Ahmed', 'PERSON'), ('Barguna', 'GPE'), ('Mahfuzur Rahman', 'PERSON'),
 ('Rozina', 'PERSON'), ('Mostafapur Bridge', 'FAC'), ('Dhaka', 'GPE'),
 ('around 12:30 pm', 'TIME'), ('Rozina', 'PERSON'), ('Mahfuzur', 'PERSON'),
 ('Madaripur Sadar Hospital', 'FAC'), ('Madaripur Sadar Police Station', 'ORG'),
 ('Rezaul Karim', 'PERSON')]

Which looks like formatted as a list of tuples of strings, but seems like the whole thing is actually of datatype str. I want to convert this to a list of tuples of strings like the way it looks and iterate through the names and entities. How can I do that?

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Nakkhatra
  • 83
  • 10

1 Answers1

2

The safest way is to use ast.literal_eval. Try not to use eval:

import ast

my_string = "[('Madaripur', 'GPE'), ('Sadar', 'GPE')]"  # Edited for brevity
my_list = ast.literal_eval(my_string)
Selcuk
  • 57,004
  • 12
  • 102
  • 110