Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'python.': 0.02; 'subject:help': 0.07; 'backwards': 0.09; 'lines:': 0.09; 'specifying': 0.09; 'subject:using': 0.09; 'subject:python': 0.11; 'files.': 0.13; 'file,': 0.15; '"from': 0.16; '-1:': 0.16; 'easier.': 0.16; 'guessing': 0.16; 'iteration,': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'input': 0.18; 'trying': 0.21; 'bit': 0.21; 'not,': 0.21; 'assumes': 0.22; 'default,': 0.22; 'occurs': 0.22; 'example': 0.23; 'proprietary': 0.23; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'appreciated.': 0.26; 'am,': 0.27; 'coding': 0.27; 'possibility': 0.27; 'lines': 0.28; 'skip:( 20': 0.28; "i'm": 0.29; 'code': 0.31; '(and': 0.32; 'file': 0.32; 'says': 0.33; 'extract': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'text': 0.34; 'loss': 0.34; 'there': 0.35; 'next': 0.35; 'but': 0.36; 'does': 0.37; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'mean': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'description': 0.39; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'help': 0.40; 'end': 0.40; 'your': 0.60; 'save': 0.61; 'subject:Need': 0.61; 'first': 0.61; 'between': 0.63; 'times': 0.63; 'email addr:gmail.com': 0.63; 'behavior': 0.64; 'dont': 0.64; 'total': 0.65; 'subject': 0.66; 'received:74.208': 0.71; 'forth': 0.75; 'apart.': 0.84 Date: Tue, 19 Mar 2013 10:54:24 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130221 Thunderbird/17.0.3 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Need help in extracting lines from word using python References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:vCf56ZJDxH8TlC1zNsfIBS1BSSoDxL4otelc9SoJLmY C/3E8+5LG/nReue39Tisgisksboev1kd2fA4Loo51Ts+/4umVC 2e92RbiljHDrzdfxqf0qSXSZf3HKhoBtUPyEsY99zNE7XrvB31 0ymeldIgqdIFnbktDrzAdRgcvkhQJMjuAxRP9iOaqQzVXbk/y8 vHw0GJ3vfkKuqLzYuv7yeoK8oJT1TJDV4pUKcel7r4hqwuVTAo TNpEPK7cS096xGqmIrAzXkD89L7hOPTg3dEI7PG5H7Af0uynIo rkD1/k0oYvtT8HmkPXM3SVQtVHdoRxlPRKgbVmAAhHvKDQSWA= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 67 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363704886 news.xs4all.nl 6864 [2001:888:2000:d::a6]:55631 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41513 On 03/19/2013 10:20 AM, razinzamada@gmail.com wrote: > I'm currently trying to extract some data between 2 lines of an input file Your subject line says "from word". I'm only guessing that you might mean Microsoft Word, a proprietary program that does not, by default, save text files. The following code and description assumes a text file, so there's a contradiction. > using Python. the infile is set up such that there is a line -START- where I need the next 10 lines of code if and only if the -END- condition occurs before the next -START-. The -START- line occurs many times before the -END-. Heres a general example of what I mean: > In other words, you want to scan for -END-, then go backwards to -START- and use the first ten of the lines between? Try coding it that way, and perhaps it'll be easier. You also need to consider (and specify behavior for) the possibility that start and end are less than 10 lines apart. > blah > blah > -START- > 10 lines I DONT need > blah > -START- > 10 lines I need > blah > blah > -END- > blah > blah > -START- > 10 lines I dont need > blah > -START- > > .... and so on and so forth > > so far I have only been able to get the -START- + 10 lines for every iteration, but am at a total loss when it comes to specifying the condition to only write if the -END- condition comes before another -START- condition. I'm a bit of a newb, so any help will be greatly appreciated. > > > heres the code I have for printing the -START- + 10 lines: > > in = open('input.log') > out = open('output.txt', 'a') > > lines = in.readlines() > for i, line in enumerate(lines): > if (line.find('START')) > -1: > out.write(line) > out.write(lines[i + 1]) > out.write(lines[i + 2]) > out.write(lines[i + 3]) > out.write(lines[i + 4]) > out.write(lines[i + 5]) > out.write(lines[i + 6]) > out.write(lines[i + 7]) > out.write(lines[i + 8]) > out.write(lines[i + 9]) > out.write(lines[i + 10]) or just out.write(lines[i:i+11) to write out all 11 of them. > -- DaveA