これは完全な答えではありませんが、開始する必要があります。
#!/bin/env python
import sys
import re
import pprint
# Some function that determines whether a line is a seperator
def is_sep(line):
return (line.count('-') > 80)
# Some function that parses the "block"
def parse_block(lines):
parsed_lines = []
for line in lines:
matches = re.findall('(\S+)', line)
parsed_lines.append(matches)
return parsed_lines
if __name__ == "__main__":
# Read in data
with open('data.txt', 'r') as fh:
data = fh.read()
# Split data into lines, then split the lines into "blocks"
blocks = []
block_lines = []
for line in data.splitlines():
if(is_sep(line)):
blocks.append(block_lines)
block_lines = []
else:
block_lines.append(line)
# This splitting method will create an empty "block" as the first element of the list, delete it
blocks = blocks[1:]
# For all blocks but the header block, pass it to "parse_block"
parsed_blocks = []
for block in blocks[1:]:
parsed_blocks.append(parse_block(block))
pprint.pprint(parsed_blocks[0])
たとえば、データの最後のブロックは次のように解析されます。
[['1', '2.6814E+03', '3.3117E+02', '1.6616E+03', '-1.1814E+02', '1.8312E+03', '3.5247E+03', '2.5879E+02', '-3.8350E+03', '0.0000E+00'],
['0.0', '2.5785E+04', '6.8687E+01', '-6.7273E+04', '-7.6310E+03', '-1.8316E+03', '-5.7811E+04', '0.0000E+00', '0.0000E+00', '0.0000E+00'],
['4.9', '1.3300E+04', '0.0000E+00', '0.0000E+00', '0.0000E+00', '9.0000E+01', '9.0000E+01', '9.0000E+01', '0.0000E+00', '1.1911E+02'],
[],
['rolling', '2.6814E+03', '3.3117E+02', '1.6616E+03', '-1.1814E+02', '1.8312E+03', '3.5247E+03', '2.5879E+02', '-3.8350E+03', '0.0000E+00'],
['averages', '2.5785E+04', '6.8687E+01', '-6.7273E+04', '-7.6310E+03', '-1.8316E+03', '-5.7811E+04', '0.0000E+00', '0.0000E+00', '0.0000E+00'],
['1.3300E+04', '0.0000E+00', '0.0000E+00', '0.0000E+00', '9.0000E+01', '9.0000E+01', '9.0000E+01', '0.0000E+00', '1.1911E+02']]