问题
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.
回答1:
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:
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.
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?
来源:https://stackoverflow.com/questions/57234705/reading-two-files-by-comparing-all-lines-of-file-2-with-each-line-of-file-1