在文本文件中交换行

时间:2016-12-02 00:21:26

标签: python-3.x

所以我有一个名为mol2文件的文件。它基本上是一个文本文件,我试图弄清楚如何交换文件中的数据行。

数据的一个例子

  1 N       -0.5660    -1.0450     0.3550 N.am  1 TRP1  -0.7300 BACKBONE
  2 H3      -0.3350    -1.9480     0.7600 H     1 TRP1   0.3700 BACKBONE
  3 CA       0.3540     0.0650     0.6210 C.3   1 TRP1   0.3610 BACKBONE
  4 HA       0.3150     0.7690    -0.2150 H     1 TRP1   0.0000 BACKBONE
  5 C        1.7800    -0.5120     0.6800 C.2   1 TRP1   0.5690 BACKBONE
  6 O        2.3250    -0.8480     1.7310 O.2   1 TRP1  -0.5700 BACKBONE
  7 CB      -0.0030     0.8580     1.8980 C.3   1 TRP1   0.1810
  8 HB1      0.8740     1.4160     2.2510 H     1 TRP1   0.0000
  9 HB2     -0.7440     1.6230     1.6310 H     1 TRP1   0.0000
 10 CG      -0.5640     0.0480     3.0300 C.2   1 TRP1  -0.1810
 11 CD1      0.1540    -0.6770     3.9600 C.2   1 TRP1  -0.3020
 12 HD1      1.2210    -0.8180     4.0790 H     1 TRP1   0.1500
 13 CD2     -1.9510    -0.1190     3.3620 C.ar  1 TRP1   0.0000
 14 NE1     -0.7260    -1.2960     4.8040 N.pl3 1 TRP1   0.0330
 15 HE1     -0.4680    -1.9020     5.5700 H     1 TRP1   0.2700
 16 CE2     -2.0170    -0.9760     4.4610 C.ar  1 TRP1  -0.1520
 17 CE3     -3.1590     0.3750     2.8250 C.ar  1 TRP1  -0.1500
 18 HE3     -3.1560     1.0570     1.9790 H     1 TRP1   0.1500
 19 CZ2     -3.2280    -1.3780     5.0370 C.ar  1 TRP1  -0.1500
 20 HZ2     -3.2550    -2.0510     5.8880 H     1 TRP1   0.1500
 21 CZ3     -4.3780    -0.0150     3.3900 C.ar  1 TRP1  -0.1500
 22 HZ3     -5.3060     0.3660     2.9720 H     1 TRP1   0.1500
 23 CH2     -4.4090    -0.8820     4.4790 C.ar  1 TRP1  -0.1500
 24 HH2     -5.3650    -1.1760     4.9040 H     1 TRP1   0.1500
 25 CA      -2.1700     0.2590    -0.9830 C.3   1 TRP1   0.0610 BACKBONE
 26 CB      -3.6350     0.1990    -1.3950 C.3   1 TRP1   0.3000
 27 HA      -2.0190     1.0810    -0.2770 H     1 TRP1   0.0000
 28 HA1     -1.5370     0.4510    -1.8560 H     1 TRP1   0.0000 BACKBONE
 29 N       -4.0840     1.4530    -1.9680 N.am  1 TRP1  -0.7300
 30 H2      -4.1770     1.5150    -2.9760 H     1 TRP1   0.3700
 31 C       -4.3910     2.5490    -1.1890 C.2   1 TRP1   0.5690
 32 C       -4.8370     3.7450    -1.9830 C.3   1 TRP1   0.0610
 33 H1      -5.4650     4.3840    -1.3570 H     1 TRP1   0.0000
 34 H2      -5.4180     3.4380    -2.8580 H     1 TRP1   0.0000
 35 H3      -3.9580     4.3060    -2.3120 H     1 TRP1   0.0000
 36 O       -4.2780     2.5770     0.0330 O.2   1 TRP1  -0.5700
 37 HB2     -3.7980    -0.5770    -2.1510 H     1 TRP1   0.0000
 38 HB3     -4.2700    -0.0350    -0.5320 H     1 TRP1   0.0000
 39 C       -1.7340    -1.0430    -0.3530 C.2   1 TRP1   0.5690 BACKBONE
 40 O       -2.3720    -2.0860    -0.4820 O.2   1 TRP1  -0.5700 BACKBONE
 41 CA       2.4750     0.9300    -2.3010 C.3   1 TRP1   0.0610 BACKBONE
 42 CB       2.0300    -0.4580    -1.8610 C.3   1 TRP1   0.3000
 43 HA       3.5610     1.0310    -2.1890 H     1 TRP1   0.0000
 44 HA1      2.0080     1.7020    -1.6790 H     1 TRP1   0.0000 BACKBONE
 45 N        2.4480    -0.7360    -0.4950 N.am  1 TRP1  -0.7300
 46 H1       3.3610    -1.1580    -0.3590 H     1 TRP1   0.3700
 47 HB2      0.9470    -0.5730    -1.9510 H     1 TRP1   0.0000
 48 HB3      2.4940    -1.2240    -2.4920 H     1 TRP1   0.0000
 49 C        2.1130     1.1940    -3.7420 C.2   1 TRP1   0.5690 BACKBONE
 50 N        2.5940     2.3870    -4.2190 N.am  1 TRP1  -0.3940 BACKBONE
 51 O        2.2950     2.6850    -5.5640 O.3   1 TRP1  -0.4360 BACKBONE
 52 H        1.7580     1.9070    -5.8290 H     1 TRP1   0.4000 BACKBONE
 53 H2       3.1370     3.0670    -3.6990 H     1 TRP1   0.3700 BACKBONE
 54 O        1.4490     0.4330    -4.4420 O.2   1 TRP1  -0.5700 BACKBONE

我想要做的是找到原子的图案

O, H, H2, O

然后一旦发现是从第一个O线获取数据,所以在这种情况下,它将以51开始的行并用从47开始的行切换它。所以输出文件将具有

 47 O        2.2950     2.6850    -5.5640 O.3   1 TRP1  -0.4360 BACKBONE 
 48 HB3      2.4940    -1.2240    -2.4920 H     1 TRP1   0.0000
 49 C        2.1130     1.1940    -3.7420 C.2   1 TRP1   0.5690 BACKBONE
 50 N        2.5940     2.3870    -4.2190 N.am  1 TRP1  -0.3940 BACKBONE
 51 HB2      0.9470    -0.5730    -1.9510 H     1 TRP1   0.0000

所以现在行51开始用输出文件

中以47开头的行切换

迄今为止的守则

def main():

   f = open('test.mol2', "r")
   newf = open('test2.mol2', "w")
   lines = f.readlines()
   for i in range(0, len(lines)):
       line = lines[i]
       if len(line) == 73 or len(line) == 64:
           if line[1] == "4" and line[2] == "7":
                line47 = line
           if line[1] == "5" and line[2] == "0":
                line50 = line
           H = lines[i + 1]
           H2 = lines[i + 2]
           O = lines[i + 3]
           if line[4] == "O" and H[4] == "H" and H2[4] == "H" and H2[5] == "2" and O[4] == "O":
               oxygen1Line = line
               oxygen2Line = O

       else:
           newf.write(line)

   f.close()

main()

它读取文件并将行存储在内存中。线47和线50存储在变量中以成为新线。下一行需要遵循O,H,H2,O的模式,然后进行交换。我在代码中处理交换时遇到了一些困难。任何想法?

0 个答案:

没有答案