0

I have two of the tuples like this :

('This is example string 1', "{'entities': [(96, 115, 'type_2')]}")
('This is example string 2', "{'entities': [(38, 42, 'type_1'), (5, 15, 'Modality'), (103, 128, 'File Name'), (58, 92, 'subtype_3')]}")

However, I want the double quotes ("") to be removed from both like this :

('This is example string 1', {'entities': [(96, 115, 'type_2')]})
('This is example string 2', {'entities': [(38, 42, 'type_1'), (5, 15, 'Modality'), (103, 128, 'File Name'), (58, 92, 'subtype_3')]})

Once it is done, the tuples should be appended to the list :

[('This is example string 1', {'entities': [(96, 115, 'type_2')]})
('This is example string 2', {'entities': [(38, 42, 'type_1'), (5, 15, 'Modality'), (103, 128, 'File Name'), (58, 92, 'subtype_3')]})]
  • Your list shows the double quotes back in the tuples. Is that what you really want? – Nick Oct 11 '22 at 05:21
  • Hi Nick, No I have edited it, I don't want double quotes, when I try to print it individually, it doesn't shows anything, as soon as I create a tuple with two strings, it starts picking up the double quotes. – Sourabh Raj Oct 11 '22 at 05:24
  • 1
    You can use `ast.literal_eval` e.g. if the tuples are in a list, use `import ast; result = [(v, ast.literal_eval(d)) for v, d in tuples]` – Nick Oct 11 '22 at 05:25
  • tuples are not in a list currently, they are appended later on.def creating_tuple(): # my_tuple = () tup_list = [] df = pd.read_excel("sheet.xlsx") for i in df.index: data_1 = df['data'][i].lstrip() data_2 = data_1.rstrip() my_tuple = (data_2, df['entity'][i]) print(my_tuple) input() – Sourabh Raj Oct 11 '22 at 05:26
  • 1
    `my_tuple = (data_2, ast.literal_eval(df['entity'][i]))` should work – Nick Oct 11 '22 at 05:28
  • 1
    You can apply like this `new_list = [ast.literal_eval(i) if '{' in i else i for i in tuple1 ]` – uozcan12 Oct 11 '22 at 05:38

0 Answers0