如果在 python 中找到匹配项,如何更新另一个文件

How to update another file if a match is found in python

这是我的问题。 我需要比较 mainfile.txt 和 transactionfile.txt 其中包含没有逗号的数字。我需要 Update/Replace mainfile.txt 内容(第 4 和第 5 列)与在 transactionfile.txt

中找到的匹配项
s1 = open("mainfile.txt", "r")
u1 = open("transactionfile.txt", "r")

for line1 in s1:
    mfile = list(line1.split()[1:5]) 
    m23 = list(line1.split()[1:3])
    m4th = list(mfile[2:4])
    m5th = list(mfile[3:4])
    print ("Main File Before Update: ", mfile)
    for line2 in u1:
        transfile = list(line2.split()[1:5]) 
        print ("After Update %s " % m23 + "%s " % transfile)
s1.close()
u1.close()

数据文件和来源

mainfile.txt (Default File Initial File)
data1 1000 8000 11 22
data2 2000 7000 33 44
data3 3000 6000 55 66
data4 4000 5000 77 88

transactionfile.txt (A text file to update the mainfile.txt)
data1 100 500
data2 200 600
data3 300 700
data4 400 800

mainfile.txt (Updated File After running the script) - Intended Output
data1 1000 8000 100 500
data2 2000 7000 200 600
data3 3000 6000 300 700
data4 4000 5000 400 800

到目前为止我的脚本不正确。非常欢迎任何意见。

Script Result: Not Correct
Main File Before Update:  ['1000', '8000', '11', '22']
After Update:             ['1000', '8000'] ['400', '800'] -> wrong
Main File Before Update:  ['2000', '7000', '33', '44']
After Update:             ['2000', '7000'] ['400', '800'] -> wrong
Main File Before Update:  ['3000', '6000', '55', '66']
After Update:             ['3000', '6000'] ['400', '800'] -> wrong
Main File Before Update:  ['4000', '5000', '77', '88']  
After Update:             ['4000', '5000'] ['400', '800'] -> wrong

您可以使用.zip()方法。 这不会覆盖文件。如果要覆盖文件,

s1.seek(0)
s1.truncate()

在第二个 for 循环之前添加这个。

主要代码是这样的:

s1 = open("mainfile.txt", "r+")
u1 = open("transactionfile.txt", "r")
all_list =[]
for line1,line2 in zip(s1,u1):
    mfile2=list(line1.split()[0:1]) #=== Fetch data1
    mfile = list(line1.split()[1:]) #=== Fetch all words after data1
    m23 = list(line1.split())
    print ("Main File Before Update: ", mfile)
    myfile2=list(line2.split()[1:])
    new_col=mfile2+mfile[:2]+myfile2 #=== Add the lists
    all_list.append(new_col)
for l in all_list:
    s1.write(" ".join(l)+"\n")
    print("Main File After Update:  ", new_col)
s1.close()
u1.close()

输出,在txt中:

data1 1000 8000 100 500
data2 2000 7000 200 600
data3 3000 6000 300 700
data4 4000 5000 400 800

这是您的解决方案。

如果你想覆盖mainfile.txt,就够了of_name

mf_name = 'mainfile.txt'
tf_name = 'transactionfile.txt'
of_name = 'output.txt'

output = []

with open(mf_name, 'r') as mf, open(tf_name, 'r') as tf:
    for ml, tl in zip(mf, tf):
        output.append(
            ' '.join(
                # The first three columns from mf_name
                ml.split()[:3] +
                # All columns from tf_name, except the first
                tl.split()[1:]
            )
        )
        
with open(of_name, 'w') as of:
    of.write('\n'.join(output))