[Tutor] Index Out of Range?List

Stephen P. Molnar s.molnar at sbcglobal.net
Wed May 3 19:32:53 EDT 2017


I have a section of Python 3 code that is intended to be part of a 
larger program which essentially inputs the file:

LOEWDIN ATOMIC CHARGES
----------------------
    0 C :   -0.780631
    1 H :    0.114577
    2 Br:    0.309802
    3 Cl:    0.357316
    4 F :   -0.001065

The code is:

import numpy as np


name = input("Enter Molecule ID: ")
name = str(name)

name_in =name[:]+'.lac.dat'
print(name_in)

atm_chg = []
"""
atm_chg = open(name_in,'r')
for line in atm_chg:
     print(line, end=' ')
"""
#all_contents = []
with open(name_in) as f:
     # skip two lines
     f.readline()
     f.readline()
     for line in f.readlines():
         atm_chg.append(float( line.split()[-1] ))

p.asarray({atm_chg})

When it is run I get:

IndexError: list index out of range

However, the Variable Explorer shows:

  [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]
[-0.780631, 0.114577, 0.309802, 0.357316, -0.001065]

One line of which is exactly what I want as input to the next step in 
the larger calculation.

Now, my question is how do I extract just one line of this file?

Thanks in advance.

-- 
Stephen P. Molnar, Ph.D.		Life is a fuzzy set
www.molecular-modeling.net		Stochastic and multivariate
(614)312-7528 (c)
Skype: smolnar1


More information about the Tutor mailing list