[Tutor] Why Doesn't the for loop Increment?

Stephen P. Molnar s.molnar at sbcglobal.net
Fri Jan 15 08:24:07 EST 2021



On 01/14/2021 04:30 PM, Cameron Simpson wrote:
> On 14Jan2021 15:33, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>> On 01/14/2021 02:35 PM, Dennis Lee Bieber wrote:
>>>> DF =       7-Phloroeckol
>>>> 0       Aloeemodin
>>>> 1  beta-Sitosterol
>>> 	I don't know pandas but that looks suspiciously like a tabular result
>>> (dataframe), where "Aloeemodin" and "beta-Sitosterol" are data items
>>> associated with a category (?) heading of "7-Phloroeckol".
> [...]
>> Upon refection, I think that you are absolutely correct. That would
>> explain why there was no incrementation. In actuality there are going
>> to be quite a few more ligands, the first round that I am contemplating
>> will be 298, to be exact. [...]
>> The question, and my problem, then becomes how do I get a list of
>> ligands into the script to extract the one number from each file?
>> A solution would be greatly appreciated.
> Well you've got 2 choices here. You could stick with the panda CSV stuff
> and put a heading on your CSV:
>
>      ligand_name
>      7-Phloroeckol
>      Aloeemodin
>      beta-Sitosterol
>
> and process the dataframe DF as a single column dataframe.
>
> Or you could do what my first instinct would be (since I've not had
> cause/time to use pandas) and _not_ treat the ligand file as a CSV.
>
>      with open("ligands") as ligands_f:
>          for line in ligands_f:
>              ligand = line.strip()
>              .... do the per-ligand stuff here ...
>
> It's just a text file with a ligand name on each line. Open it and read
> it that way.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
So close, yet so far. I would like to think that I've learned some 
lessons so far in this thread. I',m really most appreciative of the 
assistance of the group.

I kept forgetting to mention that I am using the current Spyder in an 
cvirtual environment.

I'm going a line at a time. So far:

with open('ligands_3') as ligand_file:

for ligand in ligand_file:

for num in range(1,11):

print(ligand)

name_in = "{}.{}.log".format(ligand, num)

print('NAME_IN = ',name_in)

At this point, the script does increment all of the for statements.

In Spyder I get:

NAME_IN = 7-Phloroeckol .1.log

through

NAME_IN = beta-Sitosterol .10.log

even I can see that there is obviously a line feed lurking in the RAM. . .

the next line in the script would be:

data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)

and, surprise surprise this throws an error on execution:

OSError: 7-Phloroeckol .1.log not found.


whoops! where did the line feed come from? Running dos2unix on the 
ligand file had no effect.

Well I found the line feed by running the code in a terminal.

('NAME_IN = ', '7-Phloroeckol\n.1.log')

  all the way to the end of ligands_3 (incrementing, of course.)

Now I'm really tearing out what little hair I have left - were did the 
'\n' originate??????

Sorry to be such a dunce (must be a hangover from FORTRAN II).

-- 
Stephen P. Molnar, Ph.D.
www.molecular-modeling.net
614.312.7528 (c)
Skype:  smolnar1



More information about the Tutor mailing list