Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

reposition a column

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


francesco.pietra at accademialucchese

Nov 25, 2009, 1:40 PM

Post #1 of 4 (151 views)
Permalink
reposition a column

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
Attachments: repositionRES.py (0.43 KB)


python at mrabarnett

Nov 25, 2009, 2:25 PM

Post #2 of 4 (147 views)
Permalink
Re: reposition a column [In reply to]

Francesco Pietra wrote:
> 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.
>
I'm assuming that you want to put a space where the 'W' was.

L = L[ : 17] + L[19] + L[18] + ' ' + L[20 : ]

--
http://mail.python.org/mailman/listinfo/python-list


vlastimil.brom at gmail

Nov 25, 2009, 2:32 PM

Post #3 of 4 (147 views)
Permalink
Re: reposition a column [In reply to]

2009/11/25 Francesco Pietra <francesco.pietra [at] accademialucchese>:
> 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
--
http://mail.python.org/mailman/listinfo/python-list


python at mrabarnett

Nov 26, 2009, 9:04 AM

Post #4 of 4 (132 views)
Permalink
Re: reposition a column [In reply to]

Francesco Pietra wrote:
> Hi:
>
> script now used:
>
> # Sample line
> # Slice indexes cut to the left of the corresponding item index
> # 1 2 3 4 5 6
> # 012345678901234567890123456789012345678901234567890123456789012345 ...
> # ATOM 1 W W 1 0.690 35.960 33.300 1.00 0.00
>
> data = open('out.2.5.2.5.2.0.pdb', 'r')
> outp = open('rect.out.2.5.2.5.2.0.pdb', 'w')
>
> for L in data:
> if L[19] == 'W':
> L = L[ : 17] + L[19] + L[18] + ' ' + L[20 : ]
> outp.write(L)
>
> francesco [at] tya6:~/tmp$ python MRAB.py
> Traceback (most recent call last):
> File "MRAB.py", line 11, in <module>
> if L[19] == 'W':
> IndexError: string index out of range
> francesco [at] tya6:~/tmp$
>
> Unfortunately, in my hands gmail plaint text is like that. Actually,
> the second 'W' is 19.
>
The code is assuming that the lines are all at least 19 characters long;
if a line is shorter then there's no position 19!

Try checking the line length too:

if len(L) >= 19 and L[19] == 'W':

Alternatively:

if L[19 : 20] == 'W':

(If the line is too short then L[19 : 20] will return ''.)
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.