简体   繁体   English

如何同时读取多个输入(文本)文件并进行一些计算后再次打印?

[英]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. 我有两个不同字典(单独的txt文件)的两个输入,我想逐行读取两个文件,比较并在txt文件中打印结果。 (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: 您可以使用readlines将文件读入列表,然后进行迭代以进行比较:

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. 您可以使用zip将来自不同文件的行配对在一起。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一行输入后打印文本 - How to print text after input on the same line 在python中进行一些计算后如何添加新的新列 - How to add new a new column after doing some calculation in python 从 CP 输入时同时读取两个文件 - Read two files at the same time as input from a CP 如何从同一输入打印文本,使用 Jquery 和 Flask 多次动态生成 - How to print text from the same input, dynamically generated multiple times with Jquery and Flask 如何从用户读取多个文本输入 - How to read multiple text input from user 如何从python中的多个文件中读取文本 - how to read text from multiple files in python 用tkinter选择多个文本文件后,如何同时打开和操作它们? - How to open and manipulate multiple text files all at the same time after selecting them with tkinter? Looking for a way to get input from a html form stored as python variable and after some calculation is doen, print the output too in a html page - Looking for a way to get input from a html form stored as python variable and after some calculation is doen, print the output too in a html page 如何在pandas中同时读取多个csv文件 - How to read multiple csv files at the same time in pandas 如何读取多个文本文件,我们只读取同一组的所有文本文件? - How to read multiple texts files, where we read all text files only of same group?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM