start reading from certain line

A

antar2

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!
 
T

Tim Cook

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/dev/Python+developer's+page
**************************************************************************

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

iD8DBQBIdJgQ2TFRV0OoZwMRAoVLAJ9GCFJDFj5oC2dnpRyscAXYo5/adACfW9Zs
Nv1UyhWGN5c3sEptr34bDw8=
=IUgo
-----END PGP SIGNATURE-----
 
D

Diez B. Roggisch

antar2 said:
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
 
T

Tim Cook

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/dev/Python+developer's+page
**************************************************************************

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

iD8DBQBIdJmO2TFRV0OoZwMRAuwcAJ9cTcQdyiBMxr6ictzePenLqHYd3ACgttA4
+s098hCCPN8N+WoGOzNmnfs=
=kKwH
-----END PGP SIGNATURE-----
 
N

norseman

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

if 'Item 1' in line:
print line


HTH,
Tim
=======================================================

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
(e-mail address removed)
 
J

jstrick

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()
 
I

Iain King

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top