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
  Help with to check an Input list data with a data read from an external source sacharyya 3 415 Mar-09-2024, 12:33 PM
Last Post: Pedroski55
  Extracting specific file from an archive tester_V 4 521 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extracting Data into Columns using pdfplumber arvin 17 5,590 Dec-17-2022, 11:59 AM
Last Post: arvin
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,039 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting Data from tables DataExtrator 0 1,153 Nov-02-2021, 12:24 PM
Last Post: DataExtrator
  Why changing data in a copied list changes the original list? plumberpy 3 2,251 Aug-14-2021, 02:26 AM
Last Post: plumberpy
  extracting data ajitnayak1987 1 1,541 Jul-29-2021, 06:13 AM
Last Post: bowlofred
  Extracting and printing data ajitnayak1987 0 1,412 Jul-28-2021, 09:30 AM
Last Post: ajitnayak1987
  Extracting Elements From A Website List knight2000 2 2,284 Jul-20-2021, 10:38 AM
Last Post: knight2000
  Extracting unique pairs from a data set based on another value rybina 2 2,309 Feb-12-2021, 08:36 AM
Last Post: rybina

Forum Jump:

User Panel Messages

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