I have two text files, say file1.txt contains something like
100.145 10.0728
100.298 10.04
100.212 10.0286
and file2.txt contains something like
100.223 8.92739
100.209 9.04269
100.084 9.08411
I want to compare column 1 and column 2 of both files and print match if the difference of both columns in file1.txt and in file2.txt is less or equal to 0.001.
Just read both files, split by line break, split those lines by spaces and then loop over the first files lines and for each line check whether the second files line at this position matches your condition.
with open("file1.txt", "r") as f:
f1_content = f.read()
with open("file2.txt", "r") as f:
f2_content = f.read()
f1_lines = [line.split() for line in f1_content.split("\n")]
f2_lines = [line.split() for line in f2_content.split("\n")]
for i, line in enumerate(f1_lines):
if abs(float(line[0]) - float(f2_lines[i][0])) <= 0.001 and abs(float(line[1]) - float(f2_lines[i][1])) <= 0.001:
print("Match at line {0}".format(i))
Solution using zip
as proposed by @BrianCain:
with open("file1.txt", "r") as f:
f1_content = f.read()
with open("file2.txt", "r") as f:
f2_content = f.read()
f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]
f2_lines = [line.split() for line in f2_content.split("\n") if line != ""]
for line in zip(f1_lines, f2_lines):
if abs(float(line[0][0]) - float(line[1][0])) <= 0.001 and abs(float(line[0][1]) - float(f2_lines[1][1])) <= 0.001:
print("Match at line {0}".format(line))
Requested Explanation of the solution using zip
: First we open both file (using with, as this closes the file after the block) and save the content to some variables. We use read()
instead of readlines()
bacause the latter doesnt remove an \n
when splitting at linebreaks. So i usually just use read() and split by \n
, as this does the job.
We then create ourselves for each file a list of tuples. Each entry in the list represents one line, each tuple (which is one of those entries) contains one entry per column. This is done using list comprehensions:
f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]
now we can iterate over the lines. zip
is pretty useful for that, as it can combine two lists of equal length to one list, containing tuples of the entries of the lists at the same index. Example:
zip([1, 2, 3], [4, 5, 6])
would produce
[(1, 4), (2, 5), 3, 6)]
For more look here: zip
So now we have a list, containing tuples for the lines with an entry for each file. Each entry itself contains two entries for the columns:
[(['100.145', '10.0728'], ['100.223', '8.92739']),
(['100.298', '10.04'], ['100.2985', '10.04001']),
(['100.212', '10.0286'], ['100.084', '9.08411']),
(['100.212', '100.2125'], ['100.084', '100.0845'])]
if we iterate over this list we can access files via the first [x] as in line[0] and columns via the second [y] as in line0 (accessing the first file at line at the second column). Now we simply subtract at each line the second files first column value from the first files first column value, take the absolute value (to only get positive floats) and check if its less than or equal to 0.001. The same is done for the second column, and if both are true (or one, if you use OR) then we print our message.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments