0

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)

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.

user63266
  • 23
  • 5
  • After the first time the `for j in f2` is exhausted, the cursor is at the *end* of `'file2.txt'`, and hence perfoming it a second time will not yield more results. – Willem Van Onsem Jul 27 '19 at 17:56
  • Is there a way that I can go back to the beginning of `file2.txt` and read the second line of `file1.txt` ? edit: Guy Tabak answered my question, thank you! – user63266 Jul 27 '19 at 18:01

1 Answers1

0

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.

So, that will be:

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. The split operation can be improved to one line by doing:

    f1_date, f1_number = i.split()
    
  2. 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.

  3. 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.

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.

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

Guy Tabak
  • 128
  • 7
  • So would I insert this after the `print()` function or is that incorrect? – user63266 Jul 27 '19 at 18:06
  • Yes, but outside of the loop (adding the location to my answer). – Guy Tabak Jul 27 '19 at 18:10
  • Thank you! I figured it out before you commented but thank you again! – user63266 Jul 27 '19 at 18:14
  • One more question, I have certain numeric values attached to each of the data points in the first column of each file. If I want to compare the two numeric values for when the data points that are equal in the first column how would I do that? – user63266 Jul 27 '19 at 18:16
  • Not sure what you are asking for, please clarify your question or give an example. – Guy Tabak Jul 27 '19 at 18:20
  • so the first line of `file1.txt` is `[07/30/2019, 1882]` and the first line of `file2.txt` is `[07/30/2019, 1294]` I want to compare the two numeric values if the date is the same, though I don't know how this will work, especially because the code I posted in the original question compares just the first column of both files, which is just the dates. – user63266 Jul 27 '19 at 18:31
  • I understand that I'll need an if/else function for this for loop but I'm still new to python and don't quite understand how I can make the if/else loop as well – user63266 Jul 27 '19 at 18:34
  • see updated answer, hopefully it is useful for you. – Guy Tabak Jul 27 '19 at 20:03
  • thank you so much! I will definitely go over a python tutorial very soon. – user63266 Jul 27 '19 at 20:15