strip part of string

MRAB google at mrabarnett.plus.com
Sun May 10 11:34:42 EDT 2009


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.



More information about the Python-list mailing list