Hi

I am new to Python, I would like to inquire why the script below can't use fileHandle.readline()

I am trying to store the data in an input file into a structure,
but I am not sure why
line = file.readline() fails ?

The structure contains mode. Each mode contains atom. Each atom contains x y z
There are 10 modes in the input file (indicated by ''****')
The line after '****' gives mode index and eigenval
The lines after that give
atom1.x atom1.y atom1.z atom2.x atom2.y atom2.z atom3.x
atom3.y atom3.z atom4.x atom4.y atom4.z atom5.x .....

THank you very much for advice and help!

class Mode:
  def __init__(self, index, eigenval):
    self.mode = mode
    self.index = index
    self.atom = []
    self.eigenval = val

  def getAtoms(self):
    atoms = []
    for atom in self.modes:
      atoms.append(atom)
    return atoms

class Atoms:
  def __init__(self, i, x, y, z):
    self.mode = mode
    self.i = i
    self.x = x
    self.y = y
    self.z = z
    mode.atoms.append(self)   

def storeNmaVec(fileNmaVec):
  fileHandle =open(fileNmaVec)
  modeId = 0
  modeTracker = 0
  atomCounter = 0
  vecCount1 = 1
  for line in fileHandle.readlines():
    if (line[0:5].strip() == '****'):
      modeId += 1
      print modeId 

      line = fileHandle.readline()     
      eigenval = line[9:15].strip()
      print eigenval

      for line in fileHandle.readlines():
        if (line[0:5].strip() == '****'):     
          break
        vecCount2 = splitAtomVec(line, vecCount1)
        vecCount1 = vecCount2

      mode = Mode(modeId, eigenval)
  return mode 
 
def splitAtomVec(line, vecCount):
  temp = line[2:11].split()
  print temp
  storeAtomVec(temp, vCount)
  vecCount += 1 
 
  temp = line[13:22].split()
  storeAtomVec(temp, vCount)
  vecCount += 1 
 
  temp = line[24:33].split()
  storeAtomVec(temp, vCount)
  vecCount += 1 

  temp = line[35:44].split()
  storeAtomVec(temp, vCount)
  vecCount += 1 

  temp = line[46:55].split()
  storeAtomVec(temp, vCount)
  vecCount += 1 

  temp = line[57:66].split()
  storeAtomVec(temp, vCount)
  vecCount += 1 

  temp = line[68:77].split()
  storeAtomVec(temp, vCount)
  vecCount += 1   

  return vecCount

def storeAtomVec(value, vecCount):
  if ((vecCount + 2) % 3 == 0 ):
    x = float(value)
  if ((vecCount + 1) % 3 == 0 ):
    y = float(value)
  if ((vecCount) % 3 == 0 ):
    z = float(value)
    atomCounter = vecCount / 3
    atomCounter += 1

  atom = (atomCounter, x, y, z)
     
"""
      for line in fileHandle.readlines():
        temp = line[2:11].split()
        print temp
        vecCount += 1
        if ((vecCount - 1) % 3 == 0 ):
          x = float(temp)
        if ((vecCount - 2) % 3 == 0 ):
          y = float(temp)
        if ((vecCount) % 3 == 0 ):
          z = float(temp)
        vecCount = 0 
"""     
     

"""
#causes hang
  line = fileHandle.readline()

  while line:
    if (line[0:5].strip() == '****'):
      mode += 1
      print mode 

"""
     
"""
  for line in fileHandle.readlines():
    if (line[0:5].strip() == '****'):
      mode += 1
      print mode 
    if ((modeTracker < mode) and (line[0:5].strip() != '****' ) )
"""     

 

"""
def readNmaVec(fileNmaVec):
  fileHandle = open (fileNmaVec, 'r')
  eigenvecList = []
 
  for line in fileHandle.readlines():
    if (line[0:5].strip() == '****'):     
      eigenvecList.append('****') #indicate a new NmaVec file
    else:
      eigenvec = line[2:11].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[13:22].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[24:33].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[35:44].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[46:55].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[57:66].strip()
      eigenvecList.append(eigenvec) 
      eigenvec = line[68:77].strip()
      eigenvecList.append(eigenvec) 
  #*for checking
  #*for each in eigenvecList:
    #*print each
 
  fileHandle.close()
  return eigenvecList
"""

def readPDB(fileName):
  fileHandle = open (fileName, 'r')
  atomNameList = []   

  for line in fileHandle.readlines(): 
    if (line[0:6].strip() == 'ATOM'):
      atomNameList.append(line[12:16].strip())   
  #*for checking
  #*for i in atomNameList:
    #*print i

  fileHandle.close()

  return atomNameList



#~def writeNmaVec(vecList, atomList):


if __name__ == '__main__':
  atomList = readPDB('file1.pdb')
  #eigenvecList = readNmaVec('file2.txt')
  #~writeNmaVec(eigenvecList, atomList)
  storeNmaVec('file2.txt')

line = file.readline() reads to the end of the file and then stops. Adding a "print line" statement after each readline[s] is the best way to tell what is going on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.