简体   繁体   中英

Compare two text files, replace lines in first file that contain a string from lines in second file

I'm trying to replace lines in file1 that have 'errors' with their corrected values from file2 (see below).

file1:

MAGA 0.0159
TTKI error
MCCN 0.0391
NEFD 0.9982
ESYA error

file2:

TTKI 0.7652
ESYA 0.5517

Desired output:

MAGA 0.0159
TTKI 0.7652
MCCN 0.0391
NEFD 0.9982
ESYA 0.5517

Below is how I've been trying to do it but I think I'm way off and been getting increasingly frustrated for the last hour or so, so any help would be appreciated.

section2 = []

f2 = open('file2', 'r')

for line2 in f2:
    section2.append(str(line2.split(' ',0)))

f1 = open('file1', 'r')

for line1 in f1:
    if str(section2[0]) in line1:
        print section2[0]
    else:
        print line1

You could create a dict of the correct values

dict2 = {}
for line in f2:
   key, value = line.split(' ')
   dict2[key] = value

and then

for line1 in f1:
    key, value = line1.split(' ')
    if value == 'error':
        print(key, dict2[key])
    else:
        print(line1)

Use a dictionary instead of an array:

corrections = {}

f2 = open('file2.txt', 'r')

for line2 in f2:
    (key, value) = line2.split(' ')
    corrections[key] = value

f1 = open('file1.txt', 'r')

for line1 in f1:
    (key, value) = line1.split(' ')
    if key in corrections:
        print(key, corrections[key])
    else:
        print(line1)

This is how your dictionary looks like after it read the correction file:

{'TTKI': '0.7652', 'ESYA': '0.5517'}

When file1 is read the lines are also split just to check if the first value is one of the keys in the dictionary ( key in corrections ). When it's not, the original line is just printed (even if it contains error ). But if we have a correction it is printed instead ( print (key, corrections[key] ). Here we use the fact that print inserts a space between its arguments.

Here is the simple logic:

Open the 2nd file and create a dictionary out of that. Read the file1 line/line Search in line for error, if found Get the 1st word of error line From the dictionary get the corresponding value Replace the error with the the value you got Write the line to the 3rd file

file1 = 'f1.txt'
file2 = 'f2.txt'
with open(file1, 'r') as file:
    file1data = file.read()
with open(file2, 'r') as file:
    file2data = file.read()

x = file1data.split('\n')
y = file2data.split('\n')

for i in x:
   for j in y:
      if i[:4] == j[:4]:
         file1data = file1data.replace(i,j)

with open(file1, 'w') as file:
   file.write(file1data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM