iterate start at second row in file not first

Chris cwitts at gmail.com
Wed May 21 04:39:06 EDT 2008


On May 20, 8:34 pm, notnorweg... at yahoo.se wrote:
> i have a big file with sentences, the first file of each sentence
> contains a colon(:) somewher eon that line
> i want to jump past that sentence.
>
> if all(x != ':' for x in line):
>
> this way i  can check but i dont want to check for every line in the
> whole file, quite unnecessary when i only need to
> chekc the first line.
>
> so the question is, when dealign with iterators like:
> mov = open(afile)
> for line in mov:
>         do y
>
> how do i jump the first step there? i dont want to iterate the first
> row...how do i start at the second?

mov = open(afile)
for i,line in enumerate(mov):
    if not i: # Enumeration starts at zero so it is the first line
        continue # Use continue to just move to the next iteration if
you
                 # don't want/need to do anything
        # or you can do a check for what you were looking for
        if ':' in line:
            break
    # Alternatively you could do it as
    if not i and ':' in line:
        break

    # Perform your normal operations on the file, not exactly clear
what you
    # want to perform



More information about the Python-list mailing list