We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,991 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to find and extract a block from file?

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

Attachments sample.txt (3.17KB)
2
Contributors
6
Replies
49 Minutes
Discussion Span
1 Year Ago
Last Updated
7
Views
Question
Answered
tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

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

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by Gribouillis

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 !

Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

yes thanks for the guidance.
am now reviewing:
http://www.doughellmann.com/PyMOTW/itertools/

tcl76
Light Poster
39 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0743 seconds using 2.67MB