On Jun 1, 11:11 am, Chris Rebert <c...@rebertia.com> 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) -- http://mail.python.org/mailman/listinfo/python-list