通过比较文件 2 的所有行和文件 1 的每一行来读取两个文件 [英] reading two files by comparing all lines of file 2 with each line of file 1

查看:69
本文介绍了通过比较文件 2 的所有行和文件 1 的每一行来读取两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取两个文件并比较其中包含日期的两列,如果日期相同,那么我想比较与日期相对应的两个值.我想用文件 2 的所有行读取文件 1 的一行,然后用文件 2 的所有行读取第 1 行的下一行.但是,当我尝试比较日期时,我的 for 循环读取这两个文件只运行一次.我如何制作它以便我可以像我之前所说的那样比较文件 1 和文件 2?

I'm trying to read two files and comparing two columns with dates in them and if the dates are the same, then I want to compare two values corresponding to the dates. I want to read one line of file 1 with all the lines of file 2 and then the next line of line 1 with all the lines of file 2. However, when I try to compare the dates, my for loop that reads the two files only runs once. How do I make it so that I can compare file 1 and file 2 as i said earlier?

with open('file1.txt') as f1:
with open('file2.txt') as f2:
    for i in (f1):
            column1f1 = (i.split()[0])
            column2f1 = (i.split()[1])
            for j in (f2):
                    column1f2 = (j.split()[0])
                    column2f2 = (j.split()[1])
                    print(column1f1)
                    print(column1f2)

我希望这会给我完整的文件 2 和文件 1 的第一行,然后对文件 1 的所有行重复,但它只运行文件 1 的第一行然后停止.

I expected this to give me the entirety of file 2 with the first line of file 1, and then repeated for all the lines of file 1, but instead it only runs for the first line of file 1 and then stops.

推荐答案

会发生什么,当 python 迭代第二个文件时,它会改变光标"的位置,并在迭代结束时,光标位置位于文件末尾.因此,一旦您尝试在第二次迭代中查看文件 - 它会立即终止(到达 'StopIteration'),因为光标"已经在文件的末尾.

What happens is that, when python is iterating over the second file it changes the position of the "cursor" and in the end of the iteration, the cursor location is at the end of the file. So, once you try to go over the file in the second iteration - it immediately terminates (reaches 'StopIteration') as the "cursor" is already at the end of the file.

在内循环结束时,您需要将文件当前位置(光标)返回到文件的开头.

In the end of the inner loop, you need to return the file current position (cursor for that matter) to the beginning of the file.

所以,那就是:

date_location = 0
numeric_value_location = 1
with open('file1.txt') as f1:
with open('file2.txt') as f2:
    for i in f1:
            f1_date = (i.split()[date_location])
            f1_numeric = (i.split()[numeric_value_location])
            for j in f2:
                f2_date = (j.split()[date_location])
                f2_numeric = (j.split()[numeric_value_location])
                if f1_date == f2_date:
                    if f2_numeric < f1_numeric:
                        # Do Something

            f2.seek(0, 0)

我更改了代码,希望如您所愿.请注意:

I changed the code, hopefully as you requested. Please note:

  1. 拆分操作可以改进为一行:

  1. The split operation can be improved to one line by doing:

f1_date, f1_number = i.split()

  • 我根据评论请求添加的日期比较将在某些时候中断.正确的做法是将字符串日期格式化为日期时间对象,然后进行比较.

  • The date comparison as I have added per comment request WILL BREAK at some point. The right way to do it, is to format the string date into a datetime object and then do the comparison.

    看到我已经用变量替换了位置 0, 1 的索引以赋予代码更多意义 - 尝试在将来使用这种做法.

    See that i have replaced location 0, 1 indexes with variable to give the code some more meaning - try to use this practice in the future.

    希望这就是您的要求.我强烈建议您阅读一个快速的 Python 教程,以便让自己快速入门.祝你好运.

    Hopefully, that's what you have requested. I highly recommend that you will go over a quick python tutorial just to give yourself a jump-start. Good luck.

    查看这篇文章了解更多详情:seek() 函数?

    See this post for more details: seek() function?

    这篇关于通过比较文件 2 的所有行和文件 1 的每一行来读取两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆