I have a text file that contains something like this:
# Comment
# Comment
# Comment
# Comment
# Comment
Comment
# Comment
**#Raw SIFs at Crack Propagation Step: 0**
# Vertex, X, Y, Z, K_I, K_II,
0 , 2.100000e+00 , 2.000000e+00 , -1.000000e-04 , 0.000000e+00 , 0.000000e+00 ,
1 , 2.100000e+00 , 2.000000e+00 , 1.699733e-01 , 8.727065e+00 , -8.696262e-04 ,
2 , 2.100000e+00 , 2.000000e+00 , 3.367067e-01 , 8.907810e+00 , -2.548819e-04 ,
**# MLS SIFs at Crack Propagation Step: 0**
# MLS approximation:
# Sample, t, NA, NA, K_I, K_II,
# Crack front stretch: 0
0 , 0.000000e+00 , 0.000000e+00 , 0.000000e+00 , 8.446880e+00 , -1.360875e-03 ,
1 , 5.670333e-02 , 0.000000e+00 , 0.000000e+00 , 8.554168e+00 , -1.156931e-03 ,
2 , 1.134067e-01 , 0.000000e+00 , 0.000000e+00 , 8.648241e+00 , -9.755573e-04 ,
# more comments
more comments
# more comments
**# Raw SIFs at Crack Propagation Step: 1**
# Vertex, X, Y, Z, K_I, K_II,
0 , 2.186139e+00 , 2.000000e+00 , -1.688418e-03 , 0.000000e+00 , 0.000000e+00 ,
1 , 2.192003e+00 , 2.000000e+00 , 1.646902e-01 , 9.571022e+00 , 4.770358e-03 ,
2 , 2.196234e+00 , 2.000000e+00 , 3.319183e-01 , 9.693934e+00 , -9.634989e-03 ,
**# MLS SIFs at Crack Propagation Step: 1**
# MLS approximation:
# Sample, t, NA, NA, K_I, K_II,
# Crack front stretch: 0
0 , 0.000000e+00 , 0.000000e+00 , 0.000000e+00 , 9.402031e+00 , 2.097959e-02 ,
1 , 5.546786e-02 , 0.000000e+00 , 0.000000e+00 , 9.467541e+00 , 1.443546e-02 ,
2 , 1.109357e-01 , 0.000000e+00 , 0.000000e+00 , 9.525021e+00 , 8.554051e-03 ,
As you can see, the lines without the # symbol contains the data I would like to plot. I've only shown you a short portion of step 0 and step 1, but there are around 20 steps in this file. And in each step, there are two types of data: RAW SIFS and MLS SIFS. For each section of data, I would like to plot a line graph of: vertex (1st column) versus K_I (5th column), and vertex (1st column) versus K_II (6th column)
So, in the end I would like the 20 steps of RAW SIFS for vertex vs K_I with 20 curves all in one graph. Then, another graph of the 20 steps of RAW SIFS for vertex vs K_II. Similarly, I would like the 20 steps of MLS SIFS for vertex vs K_I with 20 curves all in one graph. Then, another graph of the 20 steps of MLS SIFS for vertex vs K_II.
So far, I created a seperate text file where I just have one section of the original file. So for the Raw SIFs at Crack Propagation Step: 0 section, the code I have written uses numpy.loadtxt() to read the file:
import numpy
with open("numfile.txt") as RawStep0:
Vertex, K_I, K_II = numpy.loadtxt(RawStep0, usecols = (0, 4, 5), dtype = float, delimiter=" , ",
skiprows = 2, unpack = True)
my output --->
Vertex = array([ 0., 1., 2.])
K_I = array([0. , 8.727065, 8.90781])
How do I go about writing the code for the original file without the need to create seperate files for each section? How can I skip all those rows with the # symbol and create the arrays I need to plot?
try:
# helper function to parse a data block
def parse_SIF(lines):
SIF = []
while lines:
line = lines.pop(0).lstrip()
if line == '' or line.startswith('#'):
continue
if line.startswith('**#'):
lines.insert(0, line)
break
data = line.split(',')
# pick only columns 0, 4, 5 and
# convert to appropiate numeric format
# and append to list for current SIF and step
SIF.append([int(data[0]), float(data[4]), float(data[5])])
return SIF
# your global data structure - nested lists
raw = []
mls = []
# read whole file into one list - ok if your data is not large
with open('data') as fptr:
lines = fptr.readlines()
# global parse routine - call helper function to parse data blocks
while lines:
line = lines.pop(0)
if line.startswith('**#'):
if line.find('Raw SIFs at Crack Propagation Step:') > -1:
raw.append(parse_SIF(lines))
if line.find('MLS SIFs at Crack Propagation Step:') > -1:
mls.append(parse_SIF(lines))
# show results for your example data
from pprint import pprint
for raw_step, mls_step in zip(raw, mls):
print 'raw:'
pprint(raw_step)
print 'mls:'
pprint(mls_step)
which yields:
raw:
[[0, 0.0, 0.0], [1, 8.727065, -0.0008696262], [2, 8.90781, -0.0002548819]]
mls:
[[0, 8.44688, -0.001360875],
[1, 8.554168, -0.001156931],
[2, 8.648241, -0.0009755573]]
raw:
[[0, 0.0, 0.0], [1, 9.571022, 0.004770358], [2, 9.693934, -0.009634989]]
mls:
[[0, 9.402031, 0.02097959],
[1, 9.467541, 0.01443546],
[2, 9.525021, 0.008554051]]
This is more a general hint: Have you ever considered to use a more appropriate file format? In your use case I would recommend the hdf5 file format. There exists very nice python bindings for it: http://code.google.com/p/h5py/
Hdf5 supports sectioning and the python bindings also supports slicing and numpy. I think this would make things more easier for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.