Path: csiph.com!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.081 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'repeated': 0.07; '(possible': 0.16; '.txt': 0.16; 'file?': 0.16; 'newlines': 0.16; 'received:mail-lb0-x22e.google.com': 0.16; 'subject:between': 0.16; 'input': 0.18; '>>>': 0.20; 'keywords,': 0.22; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'handling': 0.27; 'message- id:@mail.gmail.com': 0.27; 'specify': 0.27; "skip:' 10": 0.28; 'regular': 0.29; 'e.g.': 0.30; 'skip:[ 10': 0.31; 'another': 0.32; 'add': 0.34; 'received:google.com': 0.35; 'text': 0.35; 'there': 0.36; 'possible': 0.36; 'cases': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'thank': 0.38; 'hi,': 0.38; 'to:addr:python.org': 0.40; 'easy': 0.60; 'details': 0.62; 'different': 0.63; 'between': 0.65; 'hey,': 0.75; 'to:name:python': 0.84; 'approaches,': 0.91; 'task,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=h0BDRBaYh3XwBIVVkA4wQmAMZ9i9KT4jTmlkuiZuGN0=; b=ZJ2qE2yroN1OfpFrwLyPoua2HwWzEb6IWNBUkbqaJyyQYHh0xvSlT8eg2mEcnSH3GK LBWDFuk/6uaaLhyA3LNcn1JFStOxLeIFnenkYUTfN+gE0a2fjhAU/GeAp7q6GHrNlYjt HHpugWMZqToTNuDKDioaedc+zahAwoEcwklzlZ3oGEXRMsh6yUn41kMMs2VhDj3ZwBpT ZR4htXPXOQ8qjwqtOVKwqdfqM3uzkS0XSbIwf9uibiFx8bzRi5YadVAp4NhJwS3tSzw3 Rptc8IbXEmIU8HFF1Qe1Z7uihhwQh2S01dqCxLNRVQ4oVe4OKH3vIEa3sbHoZNwvO3bq fexA== MIME-Version: 1.0 X-Received: by 10.152.178.165 with SMTP id cz5mr36006520lac.29.1441895617540; Thu, 10 Sep 2015 07:33:37 -0700 (PDT) In-Reply-To: <13d875f0-ae8d-43de-85b4-c943a0e7f5e2@googlegroups.com> References: <13d875f0-ae8d-43de-85b4-c943a0e7f5e2@googlegroups.com> Date: Thu, 10 Sep 2015 16:33:37 +0200 Subject: Re: textfile: copy between 2 keywords From: Vlastimil Brom To: python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 40 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441895625 news.xs4all.nl 23745 [2001:888:2000:d::a6]:54770 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:96261 2015-09-10 13:18 GMT+02:00 Gerald : > Hey, > > is there a easy way to copy the content between 2 unique keywords in a .txt file? > > example.txt > > 1, 2, 3, 4 > #keyword1 > 3, 4, 5, 6 > 2, 3, 4, 5 > #keyword2 > 4, 5, 6 ,7 > > > Thank you very much Hi, just to add another possible approach, you can use regular expression search for this task, e.g. (after you have read the text content to an input string): >>> import re >>> input_txt ="""1, 2, 3, 4 ... #keyword1 ... 3, 4, 5, 6 ... 2, 3, 4, 5 ... #keyword2 ... 4, 5, 6 ,7""" >>> re.findall(r"(?s)(#keyword1)(.*?)(#keyword2)", input_txt) [('#keyword1', '\n3, 4, 5, 6\n2, 3, 4, 5\n', '#keyword2')] >>> like in the other approaches, you might need to specify the details for specific cases (no keywords, only one of them, repeated keywords (possible in different order, overlapping or "crossed"), handling of newlines etc. hth, vbr