Renumbering

Terry Reedy tjreedy at udel.edu
Tue Sep 2 18:17:03 EDT 2008



Francesco Pietra wrote:
> Hi;

Hi,
Let L be a data line

> I would like to renumber, starting from 1, column 6 (i.e, 428 become
> 1, 429 becomes 2, etc for a very long list)

So you want to subtract 427 from each entry in L[22:26]

> ATOM   3424  N   LEU B 428     143.814  87.271  77.726  1.00115.20       2SG3426
>...
> ATOM   3432  N   SER B 429     142.432  85.155  78.878  1.00134.86       2SG3434
> ...
> 
> Distinctive character is column 5, i.e., it must be set that only
> lines containing "B" should be renumbered.

if L[21] ==

> As you can see, the number of lines for a particular value in column 6
> changes from situation to situation, and may even be different for the
> same name in column 4. For example, LEU can have a different number of
> lines depending on the position of this amino acid (leucine).

But from what you said earlier, this is not relevant.
> 
> I was unable to set non-proportional characters, sorry.

The display font depends on individual systems.  Thunderbird uses fixed 
pitch font, so table displays nicely for me.

> Thanks for help

This is fairly simple.  See the tutorial for explanations.

data = [
'ATOM   3424  N   LEU B 428     143.814  87.271  77.726  1.00115.20 
   2SG3426',
'ATOM   3432  N   SER B 429     142.432  85.155  78.878  1.00134.86 
   2SG3434'
]

for L in data:
   if L[21] == 'B':
     L = L[:22] + "%4d" % (int(L[22:26])-427) + L[26:]
   print(L) #py3

# prints
ATOM   3424  N   LEU B   1     143.814  87.271  77.726  1.00115.20 
  2SG3426
ATOM   3432  N   SER B   2     142.432  85.155  78.878  1.00134.86 
  2SG3434

If you start with a disk file and want to end up with a new disk file...
replace the 'data =' statement above with
    data = open('datafile','r')
follow it with
   outp = open('newdata', 'w')
and replace the print statement with
   outp.write(L)

Terry Jan Reedy




More information about the Python-list mailing list