[英]How to extract specific lines of data from a big text file
我有一個包含以下內容的文本文件:
# 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 ,
如您所見,不帶#符號的行包含我要繪制的數據。 我僅向您展示了步驟0和步驟1的一小部分,但是此文件中大約有20個步驟。 在每個步驟中,都有兩種類型的數據:RAW SIFS和MLS SIFS。 對於數據的每個部分,我想繪制一個折線圖:頂點(第一列)與K_I(第五列),以及頂點(第一列)與K_II(第六列)
因此,最后,我想在一張圖中同時包含20條曲線的RAW vs K_I的20步。 然后,繪制另一個針對頂點與K_II的RAW SIFS的20個步驟的圖形。 類似地,我想在20個曲線中將頂點與K_I的20個步驟的MLS SIFS都包含在一張圖中。 然后,另一個圖顯示了頂點vs K_II的MLS SIFS的20個步驟。
到目前為止,我已經創建了一個單獨的文本文件,其中只有原始文件的一部分。 因此,對於“ 裂紋傳播步驟:0”中的Raw SIF,我編寫的代碼使用numpy.loadtxt()讀取文件:
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)
我的輸出--->
頂點=數組([0.,1.,2.])
K_I =數組([0.,8.727065,8.90781])
我該如何為原始文件編寫代碼而無需為每個部分創建單獨的文件? 如何跳過所有帶有#符號的行並創建需要繪制的數組?
嘗試:
# 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)
產生:
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]]
這更籠統:您是否考慮過使用更合適的文件格式? 在您的用例中,我建議使用hdf5文件格式。 有非常好的python綁定: http : //code.google.com/p/h5py/
Hdf5支持分段,而python綁定也支持切片和numpy。 我認為這會使您更輕松。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.