[Tutor] building table from textfile

Ruben Guerrero rudaguerman at gmail.com
Wed Nov 27 19:22:13 CET 2013


Dear tutor.

I am trying to build the matrix and get some other information from the
following text files:

file1:
-O-
              1          2          3          4          5          6
     1  C    6.617775  -0.405794   0.371689  -0.212231   0.064402   0.064402
     2  C   -0.405794   6.617775  -0.212231   0.371689  -0.010799  -0.010799
     3  C    0.371689  -0.212231   4.887381  -0.005309   0.263318   0.263318
     4  C   -0.212231   0.371689  -0.005309   4.887381  -0.005500  -0.005500
     5  H    0.064402  -0.010799   0.263318  -0.005500   0.697750  -0.062986
     6  H    0.064402  -0.010799   0.263318  -0.005500  -0.062986   0.697750
     7  H    0.064402  -0.010799   0.263318  -0.005500  -0.062986  -0.062986
     8  H   -0.010799   0.064402  -0.005500   0.263318   0.003816  -0.001380
     9  H   -0.010799   0.064402  -0.005500   0.263318  -0.001380  -0.001380
    10  H   -0.010799   0.064402  -0.005500   0.263318  -0.001380   0.003816
              7          8          9         10
     1  C    0.064402  -0.010799  -0.010799  -0.010799
     2  C   -0.010799   0.064402   0.064402   0.064402
     3  C    0.263318  -0.005500  -0.005500  -0.005500
     4  C   -0.005500   0.263318   0.263318   0.263318
     5  H   -0.062986   0.003816  -0.001380  -0.001380
     6  H   -0.062986  -0.001380  -0.001380   0.003816
     7  H    0.697750  -0.001380   0.003816  -0.001380
     8  H   -0.001380   0.697750  -0.062986  -0.062986
     9  H    0.003816  -0.062986   0.697750  -0.062986
    10  H   -0.001380  -0.062986  -0.062986   0.697750

file 2:
-O-
              1          2          3          4          5
     1  C    5.898497   0.292009   0.177195   0.177195   0.177195
     2  H    0.292009   0.686184  -0.099114  -0.099114  -0.099114
     3  Cl   0.177195  -0.099114  17.045753  -0.129074  -0.129074
     4  Cl   0.177195  -0.099114  -0.129074  17.045753  -0.129074
     5  Cl   0.177195  -0.099114  -0.129074  -0.129074  17.045753

using the following script:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import subprocess
import numpy as np
import time
import signal

nombreInput=sys.argv[1]

inputFile = open (nombreInput,"r")
inputFileRead = inputFile.readlines()
out = open (nombreInput+".out","w")


mu = 0
nu = 0

countAtom = {
'H': 0,
'Li': 0,
'C': 0,
'N': 0,
'O': 0,
'F': 0,
'Na': 0,
'Si': 0,
'P': 0,
'S': 0,
'Cl': 0,
}
electrons = {
'H': 1,
'Li': 3,
'C': 6,
'N': 7,
'O': 8,
'F': 9,
'Na': 11,
'Si': 14,
'P': 15,
'S': 16,
'Cl': 17,
}

numberOfLines = len(inputFileRead)

maxNumberOfOrbitals = 0

# Contar el numero de orbitales
for iline in range(0,numberOfLines) :
inputFileRead[iline] = inputFileRead[iline].replace("\n","") # Quitar los
saltos de linea
# print (inputFileRead[iline])
if ( len(inputFileRead[iline].split()) == 6+2 ) :
numberOfOrbital =  int(inputFileRead[iline].split()[0])
if ( numberOfOrbital > maxNumberOfOrbitals ) :
maxNumberOfOrbitals = numberOfOrbital
# Contar los atomos

for k in range(0,maxNumberOfOrbitals) :
atom = inputFileRead[k+2].split()[1]
countAtom[atom] = countAtom[atom] + 1

# Imprimir la formula quimica
for k in countAtom :
if ( countAtom[k] > 0) :
out.write( k+"_{"+str(countAtom[k])+"}" )
out.write("\n")

# Imprimir el numero de electrones
numberOfElectrons = 0
for k in countAtom :
numberOfElectrons = numberOfElectrons + electrons[k]*countAtom[k]
out.write (str(numberOfElectrons) + "\n")

# Imprimir el numero de filas
out.write (str(maxNumberOfOrbitals) + "\n")

# Armar la matriz
matrix = np.zeros((maxNumberOfOrbitals,maxNumberOfOrbitals),dtype=float)
i = 0
m = 0
for l in range(0,numberOfLines) :
if ( len(inputFileRead[l].split()) == 6+2 ) :
for j in range(0,6) :
matrix[i][j+6*m] = float (inputFileRead[l].split()[j+2])
 if ( i + 1 == maxNumberOfOrbitals) :
i = 0
m = m + 1
else:
i = i + 1

elif ( len(inputFileRead[l].split()) == maxNumberOfOrbitals%6+2 ) :
for j in range(0,maxNumberOfOrbitals%6) :
matrix[i][j+6*m] = float (inputFileRead[l].split()[j+2])
i = i + 1
else :
i = 0

# Imprimir la matriz

for i in range(0,maxNumberOfOrbitals) :
for j in range(0,maxNumberOfOrbitals) :
out.write ("%.6f"%matrix[i][j]+" ")
out.write("\n")
#:::::::::::::::::::::::::::::::::::
# Cerrando los archivos
#:::::::::::::::::::::::::::::::::::

inputFile.close()
out.close()


but when I run teh script on file 1 I get the error:

Traceback (most recent call last):
  File "get_matrix.py", line 88, in <module>
    matrix[i][j+6*m] = float (inputFileRead[l].split()[j+2])
IndexError: index 10 is out of bounds for axis 0 with size 10

And with file 2 I don't get errors but the information in the matrix in not
in the  file.

so where is the error?


Thanks in advance,

-Ruben.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131127/7027c435/attachment-0001.html>


More information about the Tutor mailing list