473,416 Members | 1,837 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,416 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 1486
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.