[Tutor] just one question

Rich Lovely roadierich at googlemail.com
Wed Jul 15 17:29:53 CEST 2009


2009/7/15 vince spicer <vinces1979 at gmail.com>:
> one way is:
>
> import re
>
> infile = open("test.txt", "r") #: open read mode
> outfile = open("out.tx", "w") #: open write mode
>
> for line in infile:
>     values = re.split("\s+", line) # split values on spaces EX: ['47', '8',
> 'ALA', 'H', 'H', '7.85', '0.02', '1']
>     outfile.write("%s  %s C =  %s  CA =  %s CB = %s" % (values[1],
> values[2], values[5], values[6], values[7]))
>
> infile.close()
> outfile.close()
>
> not tested but should work
>
> Vince
>

That isn't what they're after at all.

Something more like

from __future__ import with_statement #only works on version 2.5 and later
from collections import defaultdict
from decimal import Decimal

atoms = defaultdict(dict)

with open("INPUT_FILENAME_HERE") as f:
    for line in f:
        n, pos, ala, at, symb, weight, rad, count = line.split()
        atoms[int(pos)][at] = Decimal(weight)


#modify these lines to fit your needs:
positionsNeeded = (8, 15, 21)
atomsNeeded = ("C", "CA", "CB")


for position in positionsNeeded:
    print position, "ALA C = %s CA = %s CB = %s" %
tuple(atoms[position].get(a,"") for a in atomsNeeded)

You would then call it with something like

$python myscipt.py > output.txt

-- 
Rich "Roadie Rich" Lovely
There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.


More information about the Tutor mailing list