Hi Amrita: I tried to figure out, for kicks, how to do what I THINK is what
you're trying to do... I've never even opened a .txt file in Python before,
so you can take all this with a big grain of salt... Anyway, if you take
your example of your original database:

1 GLY HA2=3.7850 HA3=3.9130
2 SER H=8.8500 HA=4.3370 N=115.7570
3 LYS H=8.7530 HA=4.0340 HB2=1.8080 N=123.2380
 4 LYS H=7.9100 HA=3.8620 HB2=1.7440 HG2=1.4410 N=117.9810
5 LYS H=7.4450 HA=4.0770 HB2=1.7650 HG2=1.4130 N=115.4790
6 LEU H=7.6870 HA=4.2100 HB2=1.3860 HB3=1.6050 HG=1.5130 HD11=0.7690
HD12=0.7690 HD13=0.7690 N=117.3260
7 PHE H=7.8190 HA=4.5540 HB2=3.1360 N=117.0800
8 PRO HD2=3.7450
9 GLN H=8.2350 HA=4.0120 HB2=2.1370 N=116.3660
10 ILE H=7.9790 HA=3.6970 HB=1.8800 HG21=0.8470 HG22=0.8470 HG23=0.8470
HG12=1.6010 HG13=2.1670 N=119.0300
11 ASN H=7.9470 HA=4.3690 HB3=2.5140 N=117.8620
12 PHE H=8.1910 HA=4.1920 HB2=3.1560 N=121.2640
13 LEU H=8.1330 HA=3.8170 HB3=1.7880 HG=1.5810 HD11=0.8620 HD12=0.8620
HD13=0.8620 N=119.1360

I put it in a file ashift.txt. Then:

f = open('ashift.txt', 'r')
lines = f.readlines()

Now I could iterate through lines (it is a list of strings, one per line),
but I just shortcut to play with a single line:

tshift = lines[0]
tshift = tshift.replace("=", ":")
tshift = tshift.splitlines()  # remove the final \n
tshift = tshift.split(" ")

At which point we have something like this:

['1', 'GLY', 'HA2:3.7850', 'HA3:3.9130']

I am out of time, plus I'm very conscious of doing this INCREDIBLY ineptly.
I have spent a bit of time trying to sort out the right way, there might be
some approach involving dialects associated with the csv module, but I
couldn't sort that out. If one could massage the above line (or the
original file) into

[1, 'GLY', {'HA2' : 3.7850, 'HA3' : 3.9130}]

This is what I'd talked about before, and would make reaching your final
output pretty easy, following the stuff I said above.

I KNOW there's a much, much easier way to do this, probably a one-liner (at
least for the file parsing).

You talked about printing this stuff out, but if you are going to process
it further (analyzing it in some way, for example) there might be
implications as to how you proceed with this.

Keith
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to