-1

我正在尝试编写一个函数,该函数将使用另一个文件中的文本生成一系列文件。我遇到的问题是我想将文本添加到从文件写入的行的末尾。除此之外,我希望添加的文本取决于我所在的行。

源文件如下所示: C1 1.00964527 -0.31216598 -0.03471416 C2 2.46197796 -0.03537534 -0.02239528 C3 3.13892016 -1.29949550 -0.01824029 N1 0.78031101 -1.74687208 -0.03258816 N2 2.41533961 -3.71674253 -0.03080008 H1 6.38746003 -0.16735186 0.01037509 H2 5.06643233 -2.35376889 -0.00392019 H3 2.64230377 2.15284044 -0.01822299 Cu1 -0.97960685 -2.67533229 -0.06199922 目标是生成一个输出文件,该文件在末尾有两列附加列,其中新列的值将通过函数的输入来确定。 C1 1.00964527 -0.31216598 -0.03471416 6 32 C2 2.46197796 -0.03537534 -0.02239528 4 32 C3 3.13892016 -1.29949550 -0.01824029 4 32 N1 0.78031101 -1.74687208 -0.03258816 7 32 N2 2.41533961 -3.71674253 -0.03080008 7 32 H1 6.38746003 -0.16735186 0.01037509 1 32 H2 5.06643233 -2.35376889 -0.00392019 1 32 H3 2.64230377 2.15284044 -0.01822299 1 32 Cu1 -0.97960685 -2.67533229 -0.06199922 29 32 以下代码部分复制源文件的内容并将它们粘贴到输出文件中。

def makeGNDrun(fname, aname, nAtoms):
    i = 1
    for i in range(1, nAtoms + 1):

        f = open(str(aname) + str(i) + "_gnd.run", "w+")

        with open(fname + ".xyz") as f:
            with open(aname + str(i) + "_gnd.run", "a+") as f1:
                for line in f:
                    f1.write(line)

但是,我不确定如何添加最后两列。

有什么建议么?

4

2 回答 2

0

如果我正确理解了您的问题,那么您可以执行以下操作:

def makeGNDrun(fname, aname, nAtoms):
    for i in range(1, nAtoms + 1):

        # open two files together... first in read-mode and the other in append-mode
        with open(str(aname) + str(i) + "_gnd.run", "r") as f, \
             open(aname + str(i) + "_gnd.run", "a") as f1:

             # iterate over lines of f which is in read-mode
             for line in f.readlines():
                 # line is read from f, so you can modify it before writting it to f1
                 # here I will append " i  32" to it
                 line = line.strip() + '\t' + str(i) + '\t' + str(32) + '\n'
                 f1.write(line)

希望这就是你的意思!!

于 2020-05-20T20:47:17.010 回答
0

使用 Pandas 打开文本文件,并将您自己的自定义函数应用于您需要的列,并将结果存储在另一列中。然后保存结果。

import pandas as pd

def function1(colA, colB, colC):
    return colA + colB - colC

def function2(colA, colB, colC):
    return colA * colB / colC

t = pd.read_csv(r"...\text.txt", sep='\s+', names=['idx', 'colA', 'colB', 'colC'], index_col='idx')

t["Result_1"] = t.apply(lambda x: function1(x['colA'], x['colB'], x["colC"]), axis=1)
t["Result_2"] = t.apply(lambda x: function2(x['colA'], x['colB'], x["colC"]), axis=1)

t.to_csv(r"...\Result.txt", sep='\t')

这 :

C1      1.00964527   -0.31216598   -0.03471416
C2      2.46197796   -0.03537534   -0.02239528
C3      3.13892016   -1.29949550   -0.01824029
N1      0.78031101   -1.74687208   -0.03258816
N2      2.41533961   -3.71674253   -0.03080008
H1      6.38746003   -0.16735186    0.01037509
H2      5.06643233   -2.35376889   -0.00392019
H3      2.64230377    2.15284044   -0.01822299
Cu1    -0.97960685   -2.67533229   -0.06199922

变成这样:

idx colA        colB        colC        Result_1    Result_2
C1  1.00964527  -0.31216598 -0.03471416 0.73219345  9.07920298696309
C2  2.46197796  -0.03537534 -0.02239528 2.4489979   3.8889135303289977
C3  3.13892016  -1.2994955  -0.01824029 1.8576649500000002  223.62652253770528
N1  0.78031101  -1.74687208 -0.03258816 -0.9339729099999999 41.82818290709266
N2  2.41533961  -3.71674253 -0.03080008 -1.2706028399999998 291.4666277776101
H1  6.38746003  -0.16735186 0.01037509  6.2097330799999995  -103.03075122202851
H2  5.06643233  -2.35376889 -0.00392019 2.7165836299999997  3041.9981688755424
H3  2.64230377  2.15284044  -0.01822299 4.8133672   -312.15834562936476
Cu1 -0.97960685 -2.67533229 -0.06199922 -3.59293992 -42.2710775604949
于 2020-05-20T20:51:52.780 回答