简体   繁体   中英

How to read from multiple input (text) files at the same time and print it again after doing some calculation?

I have two inputs from two different dictionaries (separate txt file), I want to read both files line by line, compare and print the result in a txt file. (in a loop) my two inputs look like this

eshark 
white 
shark
shark
carcharodon
carcharias

and

etench
tinca 
goldfish 
carassius 
auratus
great

I tried

with open('file1.txt', 'r') as f1: # for the first file 
data = f1.read()

with open('file2.txt', 'r') as f2:
data1 = f2.read() 
output = data == data1   # output is 1(true) or 0 (false)    
with open("Output1.txt", "w") as text_file 
text_file.write("word: %s :%s :%f" % (data ,data1 , output ))

I tried this as well, but same problem

with open('file1.txt') as f1,open('file2.txt') as f2:

I got the right output when my data come from one file but, when I tried with both files, I got this output:

word:shark 
white 
shark
shark
carcharodon
carcharias
:shark 

Meanwhile, I want this output

word:etench : 0
word:white : tinca : 0
word:shark : goldfish  : 0 
word:shark : carassius : 0 
word:carcharodon : auratus : 0
word:carcharias : great : 0 

You can use readlines to read the files into lists and then iterate to compare:

with open('file1.txt', 'r') as f:
    data1 = f.readlines()
with open('file2.txt', 'r') as f:
    data2 = f.readlines()
data = zip(data1, data2)
with open('output.txt', 'a') as f:
    for x in data:
        out = '{} : {} : {}\n'.format(x[0].strip(), x[1].strip(), x[0] == x[1])
        f.write(out)

This is may be an answer to your question:

with open("file1", 'r') as f1, open("file2", 'r') as f2:
    j= 0
    data2 = [k.strip("\n").strip() for k in f2.readlines()]
    for line in f1:
        if j == len(data2):
            break
        if line.strip("\n").strip() == data2[j:j+1][0]:
            output = "word:{0}:{1} = {2}".format(line.strip("\n").strip(), data2[j:j+1][0], 1)
        else:
            output = "word:{0}:{1} = {2}".format(line.strip("\n").strip(), data2[j:j+1][0], 0)
        j += 1

        with open("output_file", 'a') as out:
            out.write(output + "\n")

Output:

word:eshark:etench = 0
word:white:tinca = 0
word:shark:goldfish = 0
word:shark:carassius = 0
word:carcharodon:auratus = 0
word:carcharias:great = 0

You're basically there. Once you've read the data in though you need to iterate over each line. At the moment you're not. You can do that by using zip to pair the lines from the different files together.

Personally I'd use generators (because I love generators), but it's not necessary.

def read_lines(file_path):
    with open(file_path, 'r') as fh:
        for line in fh:
            yield line

data1 = read_lines(r"/Documents/file1.txt")
data2 = read_lines(r"/Documents/file2.txt")
data = zip(data1, data2)

with open(r"/Documents/output.txt", 'w') as fh:
    for left, right in data:
        equal = left == right
        line = "word:{left}: {right}: {equal}\n".format(left=left.strip(), 
                                                        right=right.strip(), 
                                                        equal=equal)
        fh.write(line)

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