-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compare.py
executable file
·78 lines (62 loc) · 2.51 KB
/
Compare.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Compare Two CSV Files
from os import listdir
from os.path import isdir, join
import pathlib
#decimal precision
precision = 5
def compare(directory, field):
file_list = [directory+"/"+field+'_original.csv', directory+"/"+field+'.csv'] # List of file names
output = open(directory+'/'+field+'Output.txt', 'w')
file1 = []
file2 = []
diffFound = 0
print(directory)
print(field)
# read file data
with open(file_list[0]) as f:
file1 = f.readlines()
with open(file_list[1]) as f:
file2 = f.readlines()
# print("file data read")
# remove first line from source file
file1.pop(0);
# reference matrix
rowCount = (len(file1) - 1) / 2
columnCount = (len(file1[0].strip().split(',')[:-1]) - 1) / 2
# write to file
output.write("------------------------------------\n" + "Scanner Name: " + directory + "\n------------------------------------\n" + "Field name: " + field + ":\n" + "\nMatrix: " + str(rowCount) + "cm" + " x " + str(columnCount) + "cm\n" + "Decimal precision: " + str(precision) + " decimals\n\n")
# iterate through data
for line in file1:
# row
# print(len(file1))
index = file1.index(line)
lineValues1 = line.strip().split(',')[:-1]
# remove first element from row
lineValues1.pop(0)
secondFileValues = file2[index].strip().split(',')[:-1]
for value in lineValues1:
# column
# print(len(lineValues1))
valueIndex = lineValues1.index(value)
secondValue = secondFileValues[valueIndex]
# comparison
# print(index)
# print(valueIndex)
if round(float(value.strip()), precision) != round(float(secondValue.strip()), precision):
diffFound += 1
output.write("row:"+str(index+1)+", column:"+str(valueIndex+1)+", Original value:"+ value+", Compared value:"+ secondValue+"\n")
# print("csv files compared")
# print(str(diffFound) + " difference(s) found!")
if diffFound == 0:
output.write("no differences found between files")
current_dir = pathlib.Path(__file__).parent
dirs = [f for f in listdir(current_dir) if isdir(join(current_dir, f))]
#remove directories
#dirs.remove("venv")
dirs.remove(".git")
#dirs.remove(".idea")
for directory in dirs:
fields = list(map(lambda x: x.split("_")[0], [f for f in listdir(directory) if "original" in join(current_dir, f)]))
for field in fields:
compare(directory, field)
# compare("b0")