如何获取输入文件中的输出文件格式

时间:2016-10-26 07:16:39

标签: python python-2.7

我需要在输出文件中保留输入文件的格式,但是我的代码在枚举第二列的原子后没有正确执行。如何获得输入文件中的输出文件格式?

我的代码:

from collections import defaultdict

with open('output.gro', mode='w') as f1:
    with open('input.gro', mode='r') as f2:
        f2lines=f2.readlines()
            f2line=int(f2lines[1])
            f1.write ("Good gRace! Old Maple Actually Chews Slate\n")
            f1.write ("   %d\n" % f2line)
#            print (" %d" % f2line)
            counts = defaultdict(int)
            for lines in f2lines:
                if '1LIG' in lines:
                    column = lines.split()
                    counts[column[1]] += 1
#                   print column[1]+str(counts[column[1]])
                    f1.write ("    %s      %s    %s   %.3f   %.3f   %.3f\n" % (column[0], column[1]+str(counts[column[1]]), column[2], float(column[3]), float(column[4]), float(column[5])))
            f1.write ("   0.00000   0.00000   0.00000\n")

输入文件:

Good gRace! Old Maple Actually Chews Slate
   24
    1LIG     BR    1   1.430  -2.642  -1.020
    1LIG     CL    2   1.031  -2.291  -0.877
    1LIG     CL    3   1.626  -2.468  -0.811
    1LIG      S    4   1.132  -2.341  -0.478
    1LIG      P    5   1.175  -2.166  -0.553
    1LIG      O    6   1.239  -2.163  -0.702
    1LIG      O    7   1.280  -2.073  -0.470
    1LIG      O    8   1.051  -2.062  -0.570
    1LIG      C    9   1.282  -2.272  -0.774
    1LIG      C   10   1.195  -2.340  -0.859
    1LIG      C   11   1.414  -2.312  -0.760
    1LIG      C   12   1.460  -2.422  -0.832
    1LIG      C   13   1.242  -2.450  -0.931
    1LIG      C   14   1.374  -2.491  -0.918
    1LIG      C   15   1.400  -2.130  -0.420
    1LIG      C   16   0.917  -2.110  -0.557
    1LIG      H   17   1.481  -2.258  -0.693
    1LIG      H   18   1.174  -2.504  -0.998
    1LIG      H   19   1.481  -2.111  -0.491
    1LIG      H   20   1.387  -2.238  -0.408
    1LIG      H   21   1.425  -2.086  -0.324
    1LIG      H   22   0.916  -2.218  -0.569
    1LIG      H   23   0.855  -2.064  -0.634
    1LIG      H   24   0.879  -2.084  -0.458
   0.00000   0.00000   0.0000

获得输出:

Good gRace! Old Maple Actually Chews Slate
   24
    1LIG      BR1    1   1.430   -2.642   -1.020
    1LIG      CL1    2   1.031   -2.291   -0.877
    1LIG      CL2    3   1.626   -2.468   -0.811
    1LIG      S1    4   1.132   -2.341   -0.478
    1LIG      P1    5   1.175   -2.166   -0.553
    1LIG      O1    6   1.239   -2.163   -0.702
    1LIG      O2    7   1.280   -2.073   -0.470
    1LIG      O3    8   1.051   -2.062   -0.570
    1LIG      C1    9   1.282   -2.272   -0.774
    1LIG      C2    10   1.195   -2.340   -0.859
    1LIG      C3    11   1.414   -2.312   -0.760
    1LIG      C4    12   1.460   -2.422   -0.832
    1LIG      C5    13   1.242   -2.450   -0.931
    1LIG      C6    14   1.374   -2.491   -0.918
    1LIG      C7    15   1.400   -2.130   -0.420
    1LIG      C8    16   0.917   -2.110   -0.557
    1LIG      H1    17   1.481   -2.258   -0.693
    1LIG      H2    18   1.174   -2.504   -0.998
    1LIG      H3    19   1.481   -2.111   -0.491
    1LIG      H4    20   1.387   -2.238   -0.408
    1LIG      H5    21   1.425   -2.086   -0.324
    1LIG      H6    22   0.916   -2.218   -0.569
    1LIG      H7    23   0.855   -2.064   -0.634
    1LIG      H8    24   0.879   -2.084   -0.458
   0.00000   0.00000   0.00000

所需的输出文件:

Good gRace! Old Maple Actually Chews Slate
   24
    1LIG     BR1    1   1.430  -2.642  -1.020
    1LIG     CL1    2   1.031  -2.291  -0.877
    1LIG     CL2    3   1.626  -2.468  -0.811
    1LIG      S1    4   1.132  -2.341  -0.478
    1LIG      P1    5   1.175  -2.166  -0.553
    1LIG      O1    6   1.239  -2.163  -0.702
    1LIG      O2    7   1.280  -2.073  -0.470
    1LIG      O3    8   1.051  -2.062  -0.570
    1LIG      C1    9   1.282  -2.272  -0.774
    1LIG      C2   10   1.195  -2.340  -0.859
    1LIG      C3   11   1.414  -2.312  -0.760
    1LIG      C4   12   1.460  -2.422  -0.832
    1LIG      C5   13   1.242  -2.450  -0.931
    1LIG      C6   14   1.374  -2.491  -0.918
    1LIG      C7   15   1.400  -2.130  -0.420
    1LIG      C8   16   0.917  -2.110  -0.557
    1LIG      H1   17   1.481  -2.258  -0.693
    1LIG      H2   18   1.174  -2.504  -0.998
    1LIG      H3   19   1.481  -2.111  -0.491
    1LIG      H4   20   1.387  -2.238  -0.408
    1LIG      H5   21   1.425  -2.086  -0.324
    1LIG      H6   22   0.916  -2.218  -0.569
    1LIG      H7   23   0.855  -2.064  -0.634
    1LIG      H8   24   0.879  -2.084  -0.458
   0.00000   0.00000   0.0000

1 个答案:

答案 0 :(得分:0)

SQL> select 1 as one from dual;

       ONE
----------
         1

SQL> select 1 one from dual;

       ONE
----------
         1

SQL> select 1 one from dual d;

       ONE
----------
         1

SQL> select 1 one from dual as d;
select 1 one from dual as d
                       *
ERROR at line 1:
ORA-00933: SQL command not properly ended