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

Stephen P. Molnar s.molnar at sbcglobal.net
Wed Jan 13 08:39:38 EST 2021


On 01/12/2021 04:44 PM, Cameron Simpson wrote:
> On 12Jan2021 14:32, Stephen P. Molnar <s.molnar at sbcglobal.net> wrote:
>> Python3.7.3 on Debian Buster.
>>
>> I have a large number of computational docking files from which I wish
>> to extract results.
>>
>> I have cobbled together a script that works, up to a point:
>>
>> #!/usr/bin/env python3
>> # -*- coding: utf-8 -*-
>> import pandas as pd
>> import numpy as np
>>
>> df = pd.read_csv('ligands_3')
> Did you print df here?
>
>> num = [1,2,3,4,5,6,7,8,9,10]
>>
>> for ligand in df:
> Did you print ligand here?
>
>      
>>     for i in num:
>>         name_in = "{}.{}.log".format(ligand, i)
> I'd write this as:
>
>      name_in = f"{ligand}.{i}.log"
>
> which I find more readable. Not relevant to your problem.
>
> But wait, hang on? What's in your CSV file? It looks like ligands_3 is
> just a list of ligand names? Using pandas.read_csv seems like overkill
> if so. Maybe it does do what you thought.
>
> However, looking at the pandas docs:
>
>      https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html#cookbook-csv
>
> suggests it does what you think it does.
>
> But _print out ligand_! And earlier, print out df! See what they really
> are.
>
>>         data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)
>>         name_s = ligand+'-BE'
>>         f = open(name_s, 'a')
>>         f.write(str(data[0,1])+'\n')
>>         f.close()
>>
>> For each ligand I run ten repetitions of the docking. The ligands_3
>> file contains three ligand names:
>>
>> 7-Phloroeckol
>> Aloeemodin
>> beta-Sitosterol
>>
>> The code produces, for example, for the first ligand in the list:
>>
>> -9.248153586
> [...]
>> -9.300577161
>>
>> Which happen to be correct.
>>
>> The problem, though, is that the script does not go on to the next
>> ligand in the list. Everything that I have found in Google results says
>> that the script will iterate through the list.
> The script should iterate through the list. Superficially it looks good
> to me.
>
> Print statements, they are your friend here. See what values you are
> getting at each stage.
>
> Cheers,
> Cameron Simpson <cs at cskk.id.au>
>
Thanks for the reply.

1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3import pandas as pd
4import numpy as np
5
6df = pd.read_csv('ligands_3')
7num = [1,2,3,4,5,6,7,8,9,10]
8
9for ligand in df:
10    for i in num:
11       name_in = "{}.{}.log".format(ligand, i)
12        data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)
13        name_s = ligand+'-BE'
14        f = open(name_s, 'a')
15        f.write(str(data[0,1])+'\n')
16        f.close()

I have added the line numbers above so that I can address the results of 
what I have done (s0 far) a copy of the python script is attached, as 
well as the input files that are used to test the script.

Now, I have inserted print statements in various places in the code in 
order to check what is being written.

When I run the code I get what I expected.  I commented out every line 
below each print statement. I inserted a print statement to print the  
result of line 12 and got the record that I saved in line 15. When I ran 
the complete code it printed out everything that I expected and wrote 
the file 7-Phloroeckol-BE.

At this point in the testing I ran the code line by line (printing out 
everything that happened) and got an interesting result on the execution 
of line 9 as well as the remaining lines of code.

import pandas as pd

import numpy as np

df = pd.read_csv('ligands_3')

num = [1,2,3,4,5,6,7,8,9,10]

for ligand in df:
   File "<ipython-input-5-269745371905>", line 1
     for ligand in df:
                      ^
SyntaxError: unexpected EOF while parsing


for i in num:
   File "<ipython-input-6-e91b9883e5b7>", line 1
     for i in num:
                  ^
SyntaxError: unexpected EOF while parsing


name_in = "{}.{}.log".format(ligand, i)
Traceback (most recent call last):

   File "<ipython-input-7-6ad0f8600c09>", line 1, in <module>
     name_in = "{}.{}.log".format(ligand, i)

NameError: name 'ligand' is not defined


data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)
Traceback (most recent call last):

   File "<ipython-input-8-1bf8be5d1c3e>", line 1, in <module>
     data = np.genfromtxt(name_in,skip_header=28, skip_footer=1)

NameError: name 'name_in' is not defined


name_s = ligand+'-BE'
Traceback (most recent call last):

   File "<ipython-input-9-cb0dd32021e3>", line 1, in <module>
     name_s = ligand+'-BE'

NameError: name 'ligand' is not defined


f = open(name_s, 'a')
Traceback (most recent call last):

   File "<ipython-input-10-8d5f8af45a47>", line 1, in <module>
     f = open(name_s, 'a')

NameError: name 'name_s' is not defined


f.write(str(data[0,1])+'\n')
Traceback (most recent call last):

   File "<ipython-input-11-894fa2631352>", line 1, in <module>
     f.write(str(data[0,1])+'\n')

AttributeError: 'builtin_function_or_method' object has no attribute 'write'


f.close()
Traceback (most recent call last):

   File "<ipython-input-12-b5edb7b78a9a>", line 1, in <module>
     f.close()

AttributeError: 'builtin_function_or_method' object has no attribute 'close'

I don't have the faintest idea as to what the trouble is.

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



More information about the Tutor mailing list