iterate start at second row in file not first

N

notnorwegian

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?
 
J

jay graves

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?

How about this?

mov = open(afile)
first = mov.next()
# check first for ':' and do whatever you need to do
# including the 'y' processing from below if required
for line in mov:
do y

....
Jay Graves
 
F

Falcolas

how do i jump the first step there? i dont want to iterate the first
row...how do i start at the second?

To simply bypass the first line, do a readline() on the file object
before starting to process the file. To filter out particular lines
throughout the file, you can either do a check in the for loop and
continue if it is true, or create a generator which filters out
unwanted content.

untested:

fd = open(some_file)
fi = (x for x in fd if (x.search(':') < 0))
for line in fi:
pass

It gets a bit more complicated if you want to actually split on
sentences, not lines.
 
M

Mark d.

How about:

mov = open(afile)
line = mov.readline()
....(process your ':' line)
for line in mov.readlines():
...
 
P

Paul Hankin

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?

import itertools

mov = open(afile)
for line in itertools.islice(mov, 1, None):
do y
 
C

Chris

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top