Python Forum
extracting data from a list in a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extracting data from a list in a file
#1
good morning guys,

I am trying to extract the descriptor in bold in a list (the list is in a file):

This is the content of the file: training_set_sub.txt
[['Verapamil', 'COC1=C(OC)C=C(CCN©CCCC(C#N)(C©C)C2=CC(OC)=C(OC)C=C2)C=C1', 0, 71, '0', '13', '2', 6, 7.0, 0.46, 5.09, '1', '0.60', '5.888', -2.447620829090909, '-7.093', '0'], ['Norverapamil', 'COC1=CC=C(CCNCCCC(C#N)(C©C)C2=CC(OC)=C(OC)C=C2)C=C1OC', 0, 68, '1', '13', '2', 6, 7.0, 0.46, 5.14, '1', '3.06', '5.914', -3.918865, '-7.435', '0']]

What I am doing is:

file = open('training_set_sub.txt','r')
all = file.read().replace("'",'').split('], [')
b = str(all).split(',')
print (b)

for row in all:
    print (b[3])
but this way I obtain:
71
71
and I need to obtain
71
68

Could you help me, please?
Reply
#2
It's a Python list save to text file,if have controlled how it saved it would be better to serialize to eg json,pickle.
Then is easy to get list back in original form.

Here one with ast.literal_eval a safer eval() then can get list back.
import ast

with open('list.txt') as f:
    mylist = ast.literal_eval(f.read())

print([i[3] for i in mylist])
Output:
[71, 68]
Reply
#3
Thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting data from bank statement PDFs (Accountant) a4avinash 4 5,906 Feb-27-2025, 01:53 PM
Last Post: griffinhenry
  Confused by the different ways of extracting data in DataFrame leea2024 1 713 Aug-17-2024, 01:34 PM
Last Post: deanhystad
  Extracting the correct data from a CSV file S2G 6 1,901 Jun-03-2024, 04:50 PM
Last Post: snippsat
  Help with to check an Input list data with a data read from an external source sacharyya 3 1,756 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Extracting specific file from an archive tester_V 4 2,487 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extracting Data into Columns using pdfplumber arvin 17 17,907 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Specific Lines from text file based on content. jokerfmj 8 5,722 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting Data from tables DataExtrator 0 1,649 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  Why changing data in a copied list changes the original list? plumberpy 3 3,158 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  extracting data ajitnayak1987 1 2,127 Jul-29-2021, 06:13 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020