0

I've an ASCII file written as:

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

and so on. As you can see, the file is structured in blocks of three lines each (FLAG, TIME, TYPE). I'd like to read this file and, depending on the entry 'FLAG', I want to store the related time variables and work on them etc.

I need to do this in python, but I can't read the file line-by-line, given the presence of blocks. I'm not sure about what could be the most efficient way to do that (I'm used to C++, not very skilled in python).

2
  • Do you have tried anything ? Commented Dec 5, 2016 at 10:03
  • No, in the sense... I know how to read a file line-by-line with file.open etc., but I guess here one should read a file depending on the line number or something like that... Commented Dec 5, 2016 at 10:05

3 Answers 3

2

Assuming your data file is always regular:

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

Returns

[['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']]
Sign up to request clarification or add additional context in comments.

1 Comment

Great! You reversed the problem: first read each line and then group them in blocks of three. Thanks man.
0

You can split your data every 3 lines by doing so:

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)]

That will returns:

[['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']]

3 Comments

Thanks for the reply. However, in you example, you have a single string array data that you defined as data="...". I need to read that directly from the file (it's a very large file, I cannot store everything into a single variable)
You mean I open the file, read each line and if line entry is equal to FLAG: YES, then I read the successive lines? That could be, but I don't know how to read a given number of line then...
That would be an other question
0

Simpler and not as succinct as other answers but perhaps gives you a clearer idea of what is going on.

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]

Gives:

['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 Comments

I don't think that's what the asker wants, since he said the data only have a meaning if the data is grouped by three elements.
@user312016 I agree, I've removed the generator answer and simplified it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.