hi,

attached is my sample file. i want to extract all lines starting from -- num until before -- Advanced.
appreciate if anyone can show me.

thanks
johnny

Something like

from itertools import dropwhile, takewhile

def not_advanced(line):
    return not line.startswith("-- Advanced")

def not_num(line):
    return not line.startswith("-- num")

with open("sample.txt") as lines:
    lines = takewhile(not_advanced, dropwhile(not_num, lines))
    for line in lines:
        print line

thanks but output i get is:
<itertools.takewhile object at 0x0000000002B90808>

btw, what is itertools? didnt realize got this function. i'm using Python 2.6.6

thanks but output i get is:
<itertools.takewhile object at 0x0000000002B90808>

btw, what is itertools? didnt realize got this function. i'm using Python 2.6.6

The object x returned is an iterable. You can convert to a list with list(x), or iterate with a for loop: for line in x .

Itertools is a module in a standard library which contains useful functions to manipulate iterables.

you are super!
with open("sample.txt") as lines:
lines = takewhile(not_advanced, dropwhile(not_num, lines))
print lines
for line in lines:
print line

you are super!
with open("sample.txt") as lines:
lines = takewhile(not_advanced, dropwhile(not_num, lines))
print lines
for line in lines:
print line

hehe :) Add itertools to your toolbag !

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.