I have a text file which I want to import into python and remove the comments in the beginning of it and only take the tabulated data. Also, there are empty lines after the tabulated data which I want to get rid of.
HEADING 100 m wd - spar motion at 0 m
Node segment results
MATRIX Load step and time 1 0.10 x-coor versus z-coor
TITLE x-coor versus z-coor
SUBTITLE
XLABEL x-coor
YLABEL z-coor
LEGENDS 'X-COR ''Z-COR 601'enter code here
0.000000E 00 -0.100000E 03
0.333333E 00 -0.999992E 02
0.666665E 00 -0.999984E 02
0.999998E 00 -0.999976E 02
0.133333E 01 -0.999969E 02
0.166666E 01 -0.999961E 02
0.200000E 01 -0.999953E 02
0.233333E 01 -0.999945E 02
0.266666E 01 -0.999937E 02
0.299999E 01 -0.999930E 02
0.333333E 01 -0.999922E 02
0.366666E 01 -0.999914E 02
0.399999E 01 -0.999906E 02
0.433332E 01 -0.999899E 02
And I wrote a code like this to read the file:
f = open('C:\FE_MODEL\my_file.txt','r')
f = f.readlines()
for line in f:
f = line.split()
print(f)
My expected output is
0.000000E 00 -0.100000E 03
0.333333E 00 -0.999992E 02
0.666665E 00 -0.999984E 02
0.999998E 00 -0.999976E 02
0.133333E 01 -0.999969E 02
0.166666E 01 -0.999961E 02
0.200000E 01 -0.999953E 02
0.233333E 01 -0.999945E 02
0.266666E 01 -0.999937E 02
0.299999E 01 -0.999930E 02
0.333333E 01 -0.999922E 02
0.366666E 01 -0.999914E 02
0.399999E 01 -0.999906E 02
0.433332E 01 -0.999899E 02
CodePudding user response:
If you know the header will be 8 lines long:
SKIP=8
data = []
with open("myfile.csv") as f:
for i, line in enumerate(f):
if i < SKIP:
continue
line = line.strip()
if not line:
continue
data.append([float(x) for x in line.split()])
print(data)
A few fixups here:
- use
with
blocks (context managers) to handle files, or close them explicitly (you left the file open) - iterate the file directly (will go by line), rather than reading into a list and iterating that
- cast to float, since I doubt you want strings. Remove the listcomp if you don't want to cast.
If you don't know how long the header is, we need some heuristic to tell us 'are we in a header row?' Based on what you've show above, a first guess might be 'is the row composed of numbers?':
data = []
with open("myfile.csv") as f:
for line in f:
try:
line = [float(x) for x in line.split()]
data.append(line)
except ValueError:
continue
By the way, Python also has a dedicated csv module, which would likely read your data, although for something this simple it's probably moot what you choose.
CodePudding user response:
if you already know there are always 8 lines of comments, you can try this:
f = open('file.txt')
for i in range(8):
f.readline()
for i in f.readlines():
print(i, end ='')
f.close()