473,287 Members | 1,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

iterate start at second row in file not first

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?
Jun 27 '08 #1
6 1483
On May 20, 1:34 pm, notnorweg...@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?
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
Jun 27 '08 #2
On May 20, 12:34 pm, notnorweg...@yahoo.se wrote:
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.
Jun 27 '08 #3
How about:

mov = open(afile)
line = mov.readline()
....(process your ':' line)
for line in mov.readlines():
...
On May 20, 1:34 pm, notnorweg...@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?
Jun 27 '08 #4
Mark d. wrote:
How about:

mov = open(afile)
line = mov.readline()
...(process your ':' line)
for line in mov.readlines():
...
The problem here is that you read the entire file in in the
call to readlines.

--Scott David Daniels
Sc***********@Acm.Org

Jun 27 '08 #5
On May 20, 7:34*pm, notnorweg...@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?
import itertools

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

--
Paul Hanin
Jun 27 '08 #6
On May 20, 8:34 pm, notnorweg...@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
Jun 27 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Jim Langston | last post by:
I have a class I designed that stores text chat in a std::vector<sd::string>. This class has a few methods to retrieve these strings to be displayed on the screen. void ResetRead( bool Reverse,...
0
by: Yogi_Bear_79 | last post by:
I have a form with 30 text boxes on it. They are set up in three columns, and 10 rows. Each row equals one record in the array. What I want to do is start by reading the first box txtColumnA_1,...
9
by: Tim D | last post by:
Hi, I originally posted this as a reply to a rather old thread in dotnet.framework.general and didn't get any response. I thought it might be more relevant here; anyone got any ideas? My...
28
by: John Salerno | last post by:
What is the best way of altering something (in my case, a file) while you are iterating over it? I've tried this before by accident and got an error, naturally. I'm trying to read the lines of a...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.