reposition a column
Vlastimil Brom
vlastimil.brom at gmail.com
Wed Nov 25 17:32:17 EST 2009
2009/11/25 Francesco Pietra <francesco.pietra at accademialucchese.it>:
> Hi:
>
> In a pdb file made of lines "ATOM .." (see attachment as I was unable
> to obtain plain text with gmail) I would like to reposition the second
> "W" from column 19 to 17 ( (Python numbering; in pdb numbering it
> would be 20 18). I started with bold slices, then I was unable to
> complete the script. Much obliged for help.
>
> francesco pietra
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
Hi,
using only string slices, you can probably do something like the
following (if I underestand the specification correctly, i.e. to swap
the two columns under the given condition).
An alternative is to swap the indices directly using list.
Also a regular expression replace with re.sub might be viable
(probably the shortest one)...
hth,
vbr
######################################
scale = """ 1 2 3 4 5 6
012345678901234567890123456789012345678901234567890123456789012345"""
data_line = "ATOM 1 W W 1 0.690 35.960 33.300 1.00 0.00"
if data_line [19] == 'W':
output_line = data_line [0:17]+data_line [19]+data_line
[18]+data_line [17]+data_line [20:]
# alternatively
ch_19, ch_17 = data_line [19], data_line [17]
data_lst = list(data_line)
data_lst[17] = ch_19
data_lst[19] = ch_17
output_line_2 = "".join(data_lst)
print output_line_2 == output_line
else:
output_line = data_line
print scale
print data_line
print scale
print output_line
print "=" * 66
More information about the Python-list
mailing list