如何使用python合并两个文件

时间:2016-10-21 11:41:05

标签: python

我想合并两个文件。代码应该将两个文件的第二行(input1.txt和input2.txt)相加,写入一个文件(final.txt)该求和的结果,将input2.txt中以“1MOL”开头的所有行追加到final.txt,最后将input1.txt中的所有行(前两行除外)附加到final.txt。我的代码只将“18”(两个输入文件中第二行的总和)写入final.txt。我该如何修复我的代码?

input1.txt

Molecule in solvent
13
    1MET      N    1   4.761   6.470   2.128
    1MET     H1    2   4.749   6.567   2.153
    1MET     H2    3   4.833   6.430   2.184
    1MET     H3    4   4.785   6.464   2.031
    1MET     CA    5   4.636   6.399   2.152
    1MET     HA    6   4.567   6.442   2.093
    1MET     CB    7   4.651   6.250   2.113
    1MET    HB1    8   4.730   6.213   2.162
    1MET    HB2    9   4.667   6.244   2.015
    1MET     CG   10   4.530   6.163   2.147
    1MET    HG1   11   4.452   6.219   2.119
    1MET    HG2   12   4.532   6.156   2.247
    1MET     SD   13   4.524   5.998   2.070
 spc.itp

input2.txt:

Gallium Rubidium
   5
    1MOL     CL    1   2.131   2.473   6.188
    1MOL      P    2   1.714   2.422   6.273
    1MOL      O    3   1.839   2.324   6.306
    1MOL      O    4   1.783   2.542   6.188
    1MOL      O    5   1.682   2.491   6.416

我的代码:

search_string = 'MOL'
    #read the first input file
    with open("input1.txt", mode="r") as f1:
    #open a file to merge two files
        with open("final.txt", mode="a") as f: 
            #skip the first line of the input1.txt
            line=f1.readlines()
            #take the second line of input1.txt
            a=float(line[1])
            #read the second input file
            with open("input2.txt", mode="r") as f2:
                f2lines=f2.readlines()
                b=float(f2lines[1])
                result=float(a+b)
                f.write("%.f\n" % result)
                for f2lines in f2:
                if search_string in f2lines:
                    f.write(f2lines)

所需的final.txt:

18
    1MOL     CL    1   2.131   2.473   6.188
    1MOL      P    2   1.714   2.422   6.273
    1MOL      O    3   1.839   2.324   6.306
    1MOL      O    4   1.783   2.542   6.188
    1MOL      O    5   1.682   2.491   6.416
    1MET      N    1   4.761   6.470   2.128
    1MET     H1    2   4.749   6.567   2.153
    1MET     H2    3   4.833   6.430   2.184
    1MET     H3    4   4.785   6.464   2.031
    1MET     CA    5   4.636   6.399   2.152
    1MET     HA    6   4.567   6.442   2.093
    1MET     CB    7   4.651   6.250   2.113
    1MET    HB1    8   4.730   6.213   2.162
    1MET    HB2    9   4.667   6.244   2.015
    1MET     CG   10   4.530   6.163   2.147
    1MET    HG1   11   4.452   6.219   2.119
    1MET    HG2   12   4.532   6.156   2.247
    1MET     SD   13   4.524   5.998   2.070
 spc.itp

1 个答案:

答案 0 :(得分:3)

f2lines=f2.readlines()读取f2的所有行。那么for f2lines in f2:将什么也得不到。将for f2lines in f2:循环更改为

for f2line in f2lines:
    if search_string in f2line:
        f.write(f2line)

你忘记将line中的行添加到最终文件中。你需要另一个for循环来做到这一点:

for i in xrange(2, len(line)):
    f.write(line[i])

代码是:

search_string = 'MOL'
#read the first input file    
with open("input1.txt", mode="r") as f1:
    #open a file to merge two files
    with open("final.txt", mode="w") as f: 
        #skip the first line of the input1.txt
        line=f1.readlines()
        #take the second line of input1.txt
        a=float(line[1])
        #read the second input file
        with open("input2.txt", mode="r") as f2:
            f2lines=f2.readlines()
            b=float(f2lines[1])
            result=float(a+b)
            f.write("%.f\n" % result)
            for f2line in f2lines:
                if search_string in f2line:
                    f.write(f2line)
            for i in xrange(2,len(line)):
                f.write(line[i])