value error

Terry Reedy tjreedy at udel.edu
Thu Apr 23 22:47:27 CEST 2009


Francesco Pietra wrote:
> hi:
> with script
> 
> data = open('134-176_rectified_edited.pdb', 'r')
> outp = open('134-176_renumbered.pdb', 'w')
> 
> for L in data:
>    if L[3] == 'M':
>      L = L[:24] + "%4d" % (int(L[24-28])+133) + L[28:]
>    outp.write(L)
> 
> 
> i wanted to modify lines of the type:
> ATOM      1 HH31 ACE     1       1.573   1.961   0.769  1.00  0.00           H
> 
> to add 133 to column 25, getting 134 there, and so on for next lines 2
> -> 135, 3 -> 136, etc.

A side note in addition to solution given: when writing code like this, 
to operate on column-oriented date, which I have done much of, I find it 
helpful to include in the code something like

# Sample line
#           1         2         3         4         5         6
# 012345678901234567890123456789012345678901234567890123456789012345 ...
# ATOM      1 HH31 ACE     1       1.573   1.961   0.769  1.00  0.00

Having done so...
Since slice indexes cut to the left of the corrresponding item index, I 
suspect you actually want
    L = L[:25] + "%4d" % (int(L[25:29])+133) + L[29:]
if the number is currently left-justified in its field (but note that 
%4d will *right*-justify the new number), or
    L = L[:22] + "%4d" % (int(L[22:26])+133) + L[26:]
if the number is currently right-justified.

If the data file is too big to verify correct formatting by eye, I also 
typically did preliminary checks first.  For instance, is every line 
exact the right length.  In this case, you better by sure that there are 
at most 9866 records, so you do not over-flow your four-char field.

Terry Jan Reedy




More information about the Python-list mailing list