[Tutor] Extract Field from List
Dennis Lee Bieber
wlfraed at ix.netcom.com
Sun Aug 8 12:31:41 EDT 2021
On Sun, 8 Aug 2021 09:48:00 -0400, "Stephen P. Molnar"
<s.molnar at sbcglobal.net> declaimed the following:
How many mistakes can I see...
>with open("Ligand.list_r.txt", newline='') as csvfile:
> rows = csv.reader(csvfile, delimiter = ',')
This returns an object configured to read the file -- it does not
return the rows en-mass.
> data = []
> for rows in rows:
This statement replaces the reader object by the first row of the data
-- hence it does not process the data, only the header line
> data.append(rows)
This appends the "list" of the header fields into a list, thereby
making a list containing one element -- itself a list. Since the FOR
statement trashed the CSV reader object, I'm not sure what happens on the
second pass -- either it falls out as if at end-of-input (as your output
suggests); or it pulls out the first header field, appends that as a second
list, then (given the replacement problem) pulls the first character, and
then ...
>This results in:
>
>[['CaffeicAcid\tCannflavin-A\tCannflavin-B\tCannflavin-C\tDiosmetin\tEchinacoside\tHesperetin\tL-CichoricAcid']]
>
>when run.
>
>What I need to do is iterate through the list and generate names of the
>format of chemicalname+'.i.log'.
>
The headers are the first LIST WITHIN "data"...
>This is where I can seem to hit a show stopper. How do I extract fields
>form the list generated by the script that works? What am I missing?
Indexing to get to the first list within a list?
for field in data[0]:
I suspect I'm going to be a bit offensive in my closing...
Please read the Python tutorial... Your name shows up nearly monthly
with essentially the same type of simple Python questions. You should be
able to learn Python well enough to not need to ask a question for every
variation of your "ligands" processing. This is not, to my knowledge, a
forum to do your work for you; your questions always seem to be at the
level of high-school (or even earlier) homework, which this forum does not
do either. Show us what you've tried, where it seems to go wrong, etc.; and
we provide suggestions to solve the problem. At the least, walk through
your simple code snippets with a pencil and paper -- eg: pretend to be the
computer, for each statement, write down what the contents of variables
are, etc.
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/
More information about the Tutor
mailing list