strip part of string

John Machin sjmachin at lexicon.net
Sun May 10 17:59:53 CEST 2009


On May 11, 1:34 am, MRAB <goo... at mrabarnett.plus.com> wrote:
> Francesco Pietra wrote:
> > Hi:
> > I would like to delete everything from column 54 on for each line
> > beginning with "ATOM". In the example in the attachment (sorry for the
> > attachment; I found no way with Google mail to have plain text mail)
> > the new line would become:
>
> > ATOM     49  NH1 ARG    84      84.628  41.570  44.395
>
> > without any blank space to the right. . Everything else should remain
> > at its original column. I have been looking for ???? statement, unable
> > (my big fault ) to find a right one. I tried with slices % but it
> > becomes unnecessarily complex.
>
> data = open('rec.crg', 'r')
> outp = open('rec.strip.crg', 'w')
>
> for L in data:
>     if L[3] == 'M':
>       L = L[:55].rstrip() + '\n'
>     outp.write(L)
>
> data.close()
> outp.close()
>
> Note that .rstrip() will strip all whitespace from the right-hand end of
> the string, including the '\n' at the end of the string/line, so a new
> '\n' has to be added.

His line-terminal '\n' has already disappeared before the rstrip gets
a guernsey.
His "column 54" is counting from 0, not from 1 -- his [:55] is getting
him an extra space which he doesn't want.



More information about the Python-list mailing list