473,387 Members | 1,342 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,387 software developers and data experts.

start reading from certain line

I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
....

Only the lines coming after Item 1 should be read

Thanks!
Jul 9 '08 #1
9 11018
antar2 wrote:
I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
...

Only the lines coming after Item 1 should be read
Start reading each line, and skip them until your criterion matches. Like
this:

def line_skipper(predicate, line_iterable):
for line in line_iterable:
if predicate(line):
break
for line in line_iterable:
yield line

Diez
Jul 9 '08 #2

On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
...

Only the lines coming after Item 1 should be read
file=open(filename)
while True:
line=file.readline()
if not line:
break

if 'Item 1' in line:
print line
HTH,
Tim
--
************************************************** ************************
Join the OSHIP project. It is the standards based, open source
healthcare application platform in Python.
http://www.openehr.org/wiki/display/...loper%27s+page
************************************************** ************************

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIdJgQ2TFRV0OoZwMRAoVLAJ9GCFJDFj5oC2dnpRyscA XYo5/adACfW9Zs
Nv1UyhWGN5c3sEptr34bDw8=
=IUgo
-----END PGP SIGNATURE-----

Jul 9 '08 #3
On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
...

Only the lines coming after Item 1 should be read
file=open(filename)
while True:
line=file.readline()
if not line:
break

if 'Item 1' in line:
print line
HTH,
Tim
--
************************************************** ************************
Join the OSHIP project. It is the standards based, open source
healthcare application platform in Python.
Home page: https://launchpad.net/oship/
Wiki: http://www.openehr.org/wiki/display/...loper%27s+page
************************************************** ************************

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBIdJmO2TFRV0OoZwMRAuwcAJ9cTcQdyiBMxr6ictzePe nLqHYd3ACgttA4
+s098hCCPN8N+WoGOzNmnfs=
=kKwH
-----END PGP SIGNATURE-----

Jul 9 '08 #4
On 2008-07-09, antar2 <de*******@yahoo.comwrote:
I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
...

Only the lines coming after Item 1 should be read
Not possible at most OSes, file reading always starts at the first character at
the first line. Also, most OSes don't understand the 'line' concept natively, a
file is just a long sequence of characters to them (end-of-line is also just a
character, namely '\n' (or '\r\n' if you are using Windows).

So you have to read the entire file, then throw away the bits you don't want to
keep. Luckily, Python does understand what a 'line' is, which makes the prblem
simpler.

Have a look at the readline() function (or the readlines() function if your
file is not too long). That should give you a start.

Albert
Jul 9 '08 #5
Tim Cook wrote:
On Wed, 2008-07-09 at 03:30 -0700, antar2 wrote:
>I am a starter in python and would like to write a program that reads
lines starting with a line that contains a certain word.
For example the program starts reading the program when a line is
encountered that contains 'item 1'
The weather is nice
Item 1
We will go to the seaside
...

Only the lines coming after Item 1 should be read

file=open(filename)
while True:
line=file.readline()
if not line:
break

if 'Item 1' in line:
print line
HTH,
Tim


------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
================================================== =====

I would use:

readthem= 0
file=open(filename,'r')
while readthem == 0:
line=file.readline()
if not line:
break
if 'Item 1' in line:
readthem= 1
# print line # uncomment if 'Item 1' is to be printed
while line:
line= file.readline()
print line # see note-1 below
# end of segment

The first while has lots of needed tests causing lots of bouncing about.
May even need more tests to make sure it is right tag point. As in if
'Item 1' accidentally occurred in a line previous to intended one.
The second while just buzzes on through.
If the objective is to process from tag point on then processing code
will be cleaner, easier to read in second while.

note-1:
in this form the line terminators in the file are also printed and then
print attaches it's own EOL (newline). This gives a line double spacing
effect, at least in Unix. Putting the comma at the end (print line,)
will stop that. You will need to modify two lines if used. Which to use
depends on you.
Steve
no******@hughes.net


Jul 9 '08 #6
On Wed, 09 Jul 2008 09:59:32 -0700, norseman wrote:
I would use:

readthem= 0
file=open(filename,'r')
while readthem == 0:
line=file.readline()
if not line:
break
if 'Item 1' in line:
readthem= 1
# print line # uncomment if 'Item 1' is to be printed
while line:
line= file.readline()
print line # see note-1 below
# end of segment

Ouch! That's a convoluted way of doing something which is actually very
simple. This is all you need:

outfile = open('filename', 'r')
for line in outfile:
if 'item 1' in line.lower():
print line
break
for line in outfile:
print line
If you don't like having two loops:

outfile = open('filename', 'r')
skip = True
for line in outfile:
if 'item 1' in line.lower():
skip = False
if not skip:
print line
And if you want an even shorter version:

import itertools
outfile = open('filename', 'r')
for line in itertools.dropwhile(
lambda l: 'item 1' not in l.lower(), outfile):
print line

--
Steven
Jul 10 '08 #7
Here's a simple way to do it with a minimum amount of loopiness (don't
forget to use 'try-except' or 'with' in real life):

f = open("item1.txt")

for preline in f:
if "Item 1" in preline:
print preline,
for goodline in f:
# could put an end condition with a 'break' here
print goodline,

f.close()
Jul 10 '08 #8
On Jul 10, 2:45*pm, jstrick <jstr...@mindspring.comwrote:
Here's a simple way to do it with a minimum amount of loopiness (don't
forget to use 'try-except' or 'with' in real life):

f = open("item1.txt")

for preline in f:
* * if "Item 1" in preline:
* * * * print preline,
* * * * for goodline in f:
* * * * * * # could put an end condition with a 'break' here
* * * * * * print goodline,

f.close()
No
Jul 10 '08 #9
On Jul 10, 4:54*pm, Iain King <iaink...@gmail.comwrote:
On Jul 10, 2:45*pm, jstrick <jstr...@mindspring.comwrote:
Here's a simple way to do it with a minimum amount of loopiness (don't
forget to use 'try-except' or 'with' in real life):
f = open("item1.txt")
for preline in f:
* * if "Item 1" in preline:
* * * * print preline,
* * * * for goodline in f:
* * * * * * # could put an end condition with a 'break' here
* * * * * * print goodline,
f.close()

No
Ignore that
Jul 10 '08 #10

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

Similar topics

7
by: jamait | last post by:
Hi all, I m trying to read in a text file into a datatable... Not sure on how to split up the information though, regex or substrings...? sample: Col1 Col2 ...
7
by: gyan | last post by:
I want to read a line with white spaces though scanf. So i used: scanf("%",string); above is working in one program, but in other..what may be the reason?
5
by: ltt19 | last post by:
Hi, I want to read line by line in a variable, but it could not be foward only , it need to be forward and backward, it also needs to say me which line it is reading. I do not know if it is...
2
by: Jarod | last post by:
Hey I don't know how to start reading a text file from a 30 line ? I got an idea : For i=1 to 30 streamReader.readline() Next but if it would be 2000 line it won't be a robust code... Is there...
6
by: ataanis | last post by:
Hi, Can anybody tell me , why if I change the second value in the read() function, I get an error message, I'm dealing here with a file, that has at least 240 caracters per line Private Sub...
4
abdoelmasry
by: abdoelmasry | last post by:
Hi i need help in mysql i want to specify field to start reading from EX: i have table contain this data: id username password email country 1 abdo XXXX XXXX ...
13
by: gobblegob | last post by:
Hi, I would like to know how i could read and write in a specific line in a text file with VB6. eg. first line second line third line I dont want to read through all lines, I just want to...
0
by: Nelu | last post by:
I would appreciate some comments on the code below for reading a line of text from a stream. #include <stdio.h> #include <stdlib.h> #include <stdint.h> /** * Reads a line of text from a...
1
by: Venkat143 | last post by:
Hi, I need some help with vb scripting. While setting the property of delimiters to start reading from a space of tab, i used the following code to read then write: If Variables Then ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.