使用Python 3提取和绘制列表明文数据

时间:2013-06-20 15:48:10

标签: python matplotlib extract scientific-computing plaintext

为了清楚起见,我创建了一个要点,其中包含我所指的数据格式的示例:

https://gist.github.com/TestAcc7777/5823760

在其他一些不相关的数据之后,输出文件包含许多这些列表块,一个接一个,每次都重复标题部分。每个标题实例最多有八组读数,它们一起构成一个块。标题中的数量组织反映了后续读数的值的组织。

鉴于此,我需要提取标题中给出的一些数量的值,将它们放在一个带有相关数量的文件中,并使matplotlib绘制一组数量与另一个数量。例如eng_tot与时间(ps)。

我完全迷失了,因为这远远超出了我使用Python或matplotlib的经验,所以欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,但应该让你开始。

#!/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']]