Search code examples
pythonline

How to Read the 2nd and 3rd line then Every Fifth Line After: 2nd, 3rd, 7th, 8th, 12th, 13th, etc


I am trying to read certain lines from a text file in python.

My text file:

Data Set    
for this Event    
d1: p, t, h = 9, 12, 30    
d2: p, t, h = 12, 22, 11    
Data Set     
for this Event    
d1: p, t, h = 9, 12, 30    
d2: p, t, h = 12, 22, 11

etc. 

How can I read the 2nd and 3rd line then the 7th 8th 12th 13th line so that I am only reading the lines with the data and skipping the lines that have Data Set for this Event.

Here is what I tried:

with open("d.txt") as f:

   for line2 in f.read().split("\n")[2::5]:
     print(line2)
   for line3 in f.read().split("\n")[3::5]:
     print(line3)
while line2:
   line = []
   line.append(line2)
   print(line)

But this only gives me the 2nd, 7th, 12th, 17th, etc lines. What am I doing wrong that I don't get the 2nd, 3rd, 7th, 8th, 12th, 13th, etc lines? If I write line.append(line3) instead I get the following error:

Traceback (most recent call last):
  File "write.py", line 9, in <module>
    line.append(line3)
NameError: name 'line3' is not defined

Any guidance would be much appreciated!

EDIT: After looking at the answers and comments, I now try to write the following as my code:

from itertools import tee, islice
with open("d.txt") as f:
f1 = islice(f, 2, None, 5)
f2 = islice(f ,3, None, 5)

for l1, l2 in f1, f2:
    #print(f "{l1} --- {l2}")
    pass

However, I am now getting an error that I do not understand why it is coming about: Traceback (most recent call last): File "write.py", line 6, in for l1, l2 in f1, f2: ValueError: too many values to unpack

I still don't really now how to fix this code. Any further guide would be highly appreciated!


Solution

  • The problem is that the file object f is an iterator and is therefore exhausted once you go through it.

    You can either use f.seek(0) to go back to the beginning as @user10987432 said, or you can use itertools.tee()

    itertools.tee() allows you to make multiple independent iterators from a single iterator.

    from itertools import tee, islice
    with open('foo') as f:
        f1, f2 = tee(f)
        for l1, l2 in zip(islice(f1, 2, None, 5), islice(f2, 3, None, 5)):
            print(f'{l1}  ---  {l2}')
    

    will print

    d1: p, t, h = 9, 12, 30
      ---  d2: p, t, h = 12, 22, 11
    
    d1: p, t, h = 9, 12, 30
      ---  d2: p, t, h = 12, 22, 11
    

    The \n is not stripped automatically (at least on my tests) so you will need to deal with it yourself