1

So I think this should be a pretty easy one. Say I have a text file like so.

football
baseball
basketball
soccer

and a list like so for example

colors=["orange","white"]

I understand how to iterate through them together in python

for i,j in zip(file, colors)

but is there an easy way to tell the file to start on the 3rd line so that all I would get if printed is

basketball orange
soccer white

I understand how to do this with 2 lists but I'm not sure how to tell the file to start farther down. Thanks as always, I'm still really new here and everyone is always a big help!

0

5 Answers 5

2

I would do

next(file) # get and discard
next(file)
for i,j in zip(file, colors):
    # do stuff

and then the zip operation.

If you have more than 2 or a variable number:

for _ in range(number_to_skip):
    next(file)
for i, j in zip(file, colors):
    # do stuff

This works because a file is not only iterable, but as well its own iterator. So it gives its values on its own rather than using a different iterator object (as list et al. would do).

Each next(file) gets a line from file and moves on. If you reach the "real" iteration, you are where you want to be.

2

A way to do it without converting the file into a list and keeps it in its iterable state can be achieved using itertools:

import itertools

for i, j in itertools.izip(itertools.islice(file, 2, None), colors)

The islice will skip 2 lines without converting the file into a list and keep it in its iterable state.

izip is also important, since zip might also convert things to lists, instead of keeping them iterables.

2
  • replace 3 by 2 and you are at the user's goal :-)
    – glglgl
    Dec 4, 2012 at 5:56
  • 1
    +1 for islice, but I would from itertools import izip, islice to reduce clutter - and maybe split it over a couple of lines so the logic of the skipping is separate from the for loop Dec 4, 2012 at 5:58
1

read 2 line and don't perform any action

file.readline()
file.readline()

then use normal zip

for i,j in zip(file, colors):
    # perform action
3
  • It is not good to mix readline() and iteration - they use (whyever) different techniques.
    – glglgl
    Dec 4, 2012 at 5:54
  • @glglgl: i could not understand what you meant can you please explain
    – avasal
    Dec 4, 2012 at 5:56
  • If you do next() first and then readline(), you even get a ValueError: Mixing iteration and read methods would lose data. But I don't the other direction is really supported...
    – glglgl
    Dec 4, 2012 at 6:01
0

You could do for i,j in zip(file[2:],colors)

0
0

if it will fit just use readlines to get a list of lines ..

lines = my_file.readlines()
zip(colors,lines[3:])
1
  • 1
    Perhaps because it loads everything into memory. I would find it acceptable for a small file like this, but it doesn't scale. (But I don't +1 as well...)
    – glglgl
    Dec 4, 2012 at 5:58

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.