0

I have a python script and it makes file from .dat to .csv. However, I have mistake about this script.

My code:

import os
import sys
import math

_NAME = os.path.split(sys.argv[0])[1]   #who am i?
_TOR = 1e-10

if len(sys.argv)>2:
    datFile=sys.argv[1]
    outFile=sys.argv[2]

else:
    print("usage > python  %s  infile   outfile    " % _NAME)
    print("   ex > python  %s  ndisp.dat ndisp.csv " % _NAME)
    sys.exit() 

lineCount=0

def ReadInp(fi):
    global lineCount
    s=fi.readline()
    if (s!=""):
        lineCount=lineCount+1
    return s

"""
 displacements (vx,vy,vz) for set NDISPI1 and time  0.1562500E-01

   255 -4.3462E-05  1.4730E-04  0.0000E+00
  1431 -4.1070E-05  0.0000E+00  0.0000E+00

 displacements (vx,vy,vz) for set NDISPO1 and time  0.1562500E-01

      2733  1.0723E-04 -4.4200E-05  0.0000E+00
  2880  1.0488E-04  0.0000E+00  0.0000E+00
"""

fi = open(datFile,'r')
fo = open(outFile,'w')

sFind1="DISPLACEMENTS (VX,VY,VZ) FOR SET"
sFind2="AND TIME"
nFind1=len(sFind1)
nFind2=len(sFind2)

s1=""
sName=""
sTimeOld=""
sTime=""
ss=""
flgHeader=False
while True:
    s=ReadInp(fi)
    if (s==""):
        break
s1=s.strip() #chomp
s2=s1.split()
n2=len(s2)
iName1=s1.upper().find(sFind1)
if (iName1 != -1 ):
    iName2=s1.upper().find(sFind2)
    sName=s1[iName1+nFind1:iName2].strip()
    sTime=s1[iName2+nFind2:].strip()
    if( (sTime != sTimeOld)):
        if( sTimeOld != "" ):
            if (flgHeader==False):
                flgHeader=True
                ns=int(len(ss.split(",")))/7
                sHeader="Name,Time,Node,ux,uy,uz,uAll,"*ns
                fo.write(sHeader)
                fo.write("\n")
            fo.write(ss)
            fo.write("\n")
            ss=""
            print(sTime)
        sTimeOld=sTime
elif ( n2 == 4 ):
    Node=int(s2[0])
    ux=float(s2[1])
    uy=float(s2[2])
    uz=float(s2[3])
    uAll=math.sqrt(ux*ux+uy*uy+uz*uz)

    ss=ss+"%s,%s,%d,%g,%g,%g,%g," % (sName , sTime , Node,ux,uy,uz,uAll )

if(ss != "" ):
    fo.write(ss)
    fo.write("\n")


fi.close()
fo.close()

What is my wrong about running this code?

My mistake is:

Traceback (most recent call last):
  File "do2csv.py", line 105, in <module>
    ux=float(s2[1])
ValueError: could not convert string to float: NO

My input in .dat file:

    E I G E N V A L U E   O U T P U T
 MODE NO  EIGENVALUE             FREQUENCY
                     (RAD/TIME)      (CYCLES/TIME)
1   0.8040979E+04   0.8967150E+02   0.1427166E+02

2   0.8040979E+04   0.8967151E+02   0.1427166E+02

3   0.3158085E+06   0.5619685E+03   0.8944006E+02      

4   0.3158085E+06   0.5619685E+03   0.8944006E+02

5   0.2476525E+07   0.1573698E+04   0.2504618E+03

6   0.2476525E+07   0.1573698E+04   0.2504618E+03

7   0.9513950E+07   0.3084469E+04   0.4909085E+03

8   0.9513950E+07   0.3084469E+04   0.4909085E+03

9   0.2601478E+08   0.5100468E+04   0.8117648E+03

10   0.2601478E+08   0.5100468E+04   0.8117648E+03
5
  • Please tell us what is wrong with the code. What input do you give it, what output do you get, what did you expect instead? What errors, if any, did you see? Without that information, we are sailing blind and unlikely to help. Commented Nov 27, 2013 at 13:12
  • 1
    I've also trimmed some of the comments and prints in your code to make it a little more compact. Please remove anything that is not required for us to reproduce the problem. Commented Nov 27, 2013 at 13:12
  • I added my error and you delete it. Commented Nov 27, 2013 at 13:12
  • That was a simple editing clash, my apologies. I've added that in. Commented Nov 27, 2013 at 13:14
  • I added my input file. I want to convert this .dat file to .csv file. Commented Nov 27, 2013 at 13:23

1 Answer 1

1

If I am reading this correctly, your code assumes that any line with four fields (n2 == 4) will be a line of numeric data. But that's not true: one of the header lines of the table also has just four fields.

You could avoid this problem by skipping any line that starts with a letter, or contains a non-numeric field anywhere in it, or just skipping the first three lines of the file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.