Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder4.news.weretis.net!feeder.news-service.com!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.134.4.91.MISMATCH!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '(at': 0.03; 'wed,': 0.03; 'string.': 0.04; 'extracted': 0.05; 'exec': 0.07; 'python': 0.08; 'expressions.': 0.09; 'trailing': 0.09; 'pm,': 0.10; 'output': 0.11; '>>>': 0.12; 'am,': 0.14; 'wrote:': 0.14; 'file")': 0.16; 'from:addr:free.fr': 0.16; 'issue)': 0.16; 'rakesh': 0.16; 'rebert': 0.16; 'received:212.27': 0.16; 'received:212.27.42': 0.16; 'received:free.fr': 0.16; 'slicing': 0.16; 'cc:addr:python- list': 0.17; '(perhaps': 0.19; 'solution.': 0.19; 'header:In- Reply-To:1': 0.21; 'seems': 0.21; 'cc:2**0': 0.22; 'cc:no real name:2**0': 0.23; 'extract': 0.25; 'fixed': 0.27; 'script': 0.27; 'example': 0.27; '(the': 0.28; 'problem': 0.28; 'subject:how': 0.29; 'import': 0.29; 'odd': 0.29; 'skip:( 20': 0.30; 'cc:addr:python.org': 0.30; 'url:library': 0.31; 'print': 0.31; "skip:' 10": 0.32; 'list': 0.33; 'starting': 0.33; 'too': 0.33; 'regular': 0.34; 'chris': 0.34; 'file': 0.34; 'header:User- Agent:1': 0.35; 'typical': 0.35; 'open': 0.36; 'url:docs': 0.37; 'another': 0.37; 'url:python': 0.38; 'url:org': 0.38; 'but': 0.38; 'data': 0.38; 'subject:: ': 0.38; 'some': 0.38; 'received:192': 0.38; 'rest': 0.39; 'best': 0.60; 'your': 0.60; 'email addr:yahoo.com': 0.63; 'positions': 0.64; 'relevant': 0.69; 'spaces': 0.73; 'to:addr:yahoo.com': 0.80; 'spaces.': 0.84 Date: Wed, 01 Jun 2011 22:34:19 +0200 From: Karim User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.17) Gecko/20110424 Thunderbird/3.1.10 MIME-Version: 1.0 To: "rurpy@yahoo.com" Subject: Re: how to avoid leading white spaces References: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> In-Reply-To: <9e861b0e-e768-401b-b5ca-190f20830a08@s9g2000yqm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 52 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1306960466 news.xs4all.nl 49183 [::ffff:82.94.164.166]:45192 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:6823 On 06/01/2011 09:39 PM, rurpy@yahoo.com wrote: > On Jun 1, 11:11 am, Chris Rebert wrote: >> On Wed, Jun 1, 2011 at 12:31 AM, rakesh kumar >>> Hi >>> >>> i have a file which contains data >>> >>> //ACCDJ EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCDJ ' >>> //ACCT EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCT ' >>> //ACCUM EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM ' >>> //ACCUM1 EXEC DB2UNLDC,DFLID=&DFLID,PARMLIB=&PARMLIB, >>> // UNLDSYST=&UNLDSYST,DATABAS=MBQV1D0A,TABLE='ACCUM1 ' >>> >>> i want to cut the white spaces which are in between single quotes after TABLE=. >>> >>> for example : >>> 'ACCT[spaces] ' >>> 'ACCUM ' >>> 'ACCUM1 ' >>> the above is the output of another python script but its having a leading spaces. >> Er, you mean trailing spaces. Since this is easy enough to be >> homework, I will only give an outline: >> >> 1. Use str.index() and str.rindex() to find the positions of the >> starting and ending single-quotes in the line. >> 2. Use slicing to extract the inside of the quoted string. >> 3. Use str.rstrip() to remove the trailing spaces from the extracted string. >> 4. Use slicing and concatenation to join together the rest of the line >> with the now-stripped inner string. >> >> Relevant docs:http://docs.python.org/library/stdtypes.html#string-methods > For some odd reason (perhaps because they are used a lot in Perl), > this groups seems to have a great aversion to regular expressions. > Too bad because this is a typical problem where their use is the > best solution. > > import re > f = open ("your file") > for line in f: > fixed = re.sub (r"(TABLE='\S+)\s+'$", r"\1'", line) > print fixed, > > (The above is for Python-2, adjust as needed for Python-3) Rurpy, Your solution is neat. Simple is better than complicated... (at list for this simple issue)