在python中读取带有内部块的文本文件

时间:2016-12-05 10:00:35

标签: python file ascii

我的ASCII文件写成:

FLAG: YES
TIME: 2016/04/06 12:07:51.249999 YELLOW
TYPE: FLEX
FLAG: NO
TIME: 2016/04/06 12:51:17.455175 YELLOW
TYPE: FLEX
FLAG: YES
TIME: 2016/04/06 13:49:04.999987 BLUE
TYPE: NON-FLEX
FLAG: YES
TIME: 2016/04/06 15:59:25.255675 GREEN
TYPE: BETA

等等。如您所见,文件的结构分为三行(FLAG,TIME,TYPE)。我想阅读此文件,并根据条目' FLAG',我想存储相关的时间变量并对其进行处理等。

我需要在python中执行此操作,但鉴于存在块,我无法逐行读取文件。我不确定什么是最有效的方法(我已经习惯了C ++,在python方面不是很熟练)。

3 个答案:

答案 0 :(得分:2)

假设您的数据文件始终是常规的:

with open('data.txt') as f:
    lines = f.read().split('\n')
blocks = [lines[i:i+3] for i in range(0, len(lines), 3)]

返回

[['FLAG: YES', 'TIME: 2016/04/06 12:07:51.249999 YELLOW', 'TYPE: FLEX'],
 ['FLAG: NO', 'TIME: 2016/04/06 12:51:17.455175 YELLOW', 'TYPE: FLEX'],
 ['FLAG: YES', 'TIME: 2016/04/06 13:49:04.999987 BLUE', 'TYPE: NON-FLEX'],
 ['FLAG: YES', 'TIME: 2016/04/06 15:59:25.255675 GREEN', 'TYPE: BETA']]

答案 1 :(得分:0)

您可以通过这样做每3行拆分数据:

data = """FLAG: YES
TIME: 2016/04/06 12:07:51.249999 YELLOW
TYPE: FLEX
FLAG: NO
TIME: 2016/04/06 12:51:17.455175 YELLOW
TYPE: FLEX
FLAG: YES
TIME: 2016/04/06 13:49:04.999987 BLUE
TYPE: NON-FLEX
FLAG: YES
TIME: 2016/04/06 15:59:25.255675 GREEN
TYPE: BETA"""

data = [data[_:_+3] for _ in range(0, len(data.split('\n')), 3)]

那将返回:

[['FLAG: YES', 'TIME: 2016/04/06 12:07:51.249999 YELLOW', 'TYPE: FLEX'], ['FLAG: NO', 'TIME: 2016/04/06 12:51:17.455175 YELLOW', 'TYPE: FLEX'], ['FLAG: YES', 'TIME: 2016/04/06 13:49:04.999987 BLUE', 'TYPE: NON-FLEX'], ['FLAG: YES', 'TIME: 2016/04/06 15:59:25.255675 GREEN', 'TYPE: BETA']]

答案 2 :(得分:0)

更简单,并不像其他答案那样简洁,但也许会让你更清楚地知道发生了什么。

f = open('data.txt','r')
content=[]
data=[]
while True:
    for i in range(3):          #read file lines in blocks of 3
        content.append(f.readline().strip('\n'))
    if content == ['','','']:   #If there is no data break from loop
        break
    data.append(content)        #Add contents to main list
    content=[]                  # clear for next 3 lines of data
f.close()
for i in range(len(data)):
    print data[i]

给出:

['FLAG: YES', 'TIME: 2016/04/06 12:07:51.249999 YELLOW', 'TYPE: FLEX']
['FLAG: NO', 'TIME: 2016/04/06 12:51:17.455175 YELLOW', 'TYPE: FLEX']
['FLAG: YES', 'TIME: 2016/04/06 13:49:04.999987 BLUE', 'TYPE: NON-FLEX']
['FLAG: YES', 'TIME: 2016/04/06 15:59:25.255675 GREEN', 'TYPE: BETA']