| 1 |
nino.borges |
912 |
"""
|
| 2 |
|
|
|
| 3 |
|
|
ATT_CSV_Compare
|
| 4 |
|
|
|
| 5 |
|
|
Created by:
|
| 6 |
|
|
Emanuel Borges
|
| 7 |
|
|
07.21.2025
|
| 8 |
|
|
|
| 9 |
|
|
A simple program that will take two CSV files, formatted for Excel or Excel-generated and compares the values.
|
| 10 |
|
|
In this instance it was a two value CSV where the first value was the bates and the key between the files.
|
| 11 |
|
|
|
| 12 |
|
|
"""
|
| 13 |
|
|
|
| 14 |
|
|
import csv
|
| 15 |
|
|
|
| 16 |
|
|
|
| 17 |
|
|
def csv_to_dict(fileName):
|
| 18 |
|
|
result = {}
|
| 19 |
|
|
with open(fileName, mode='r', encoding='utf-8', newline='') as csvfile:
|
| 20 |
|
|
reader = csv.reader(csvfile)
|
| 21 |
|
|
next(reader) ## Skipping the header row
|
| 22 |
|
|
for row in reader:
|
| 23 |
|
|
if len(row) >= 2:
|
| 24 |
|
|
key = row[0].upper()
|
| 25 |
|
|
value = row[1]
|
| 26 |
|
|
result[key] = value
|
| 27 |
|
|
else:
|
| 28 |
|
|
print("ERROR: Bad Row")
|
| 29 |
|
|
return result
|
| 30 |
|
|
|
| 31 |
|
|
|
| 32 |
|
|
def key_field_verify(matrix):
|
| 33 |
|
|
"""Verifies that the key field starts with either SHINY or VRMOT"""
|
| 34 |
|
|
for k in list(matrix.keys()):
|
| 35 |
|
|
if k[:5] != "SHINY":
|
| 36 |
|
|
if k[:5] != "VRMOT":
|
| 37 |
|
|
print(f"ERROR: {k} is not valid key.")
|
| 38 |
|
|
|
| 39 |
|
|
|
| 40 |
|
|
def compare_dicts(dict1, dict2, output_file):
|
| 41 |
|
|
"""Compares the two dictionaries, generating a report of the differences which includes where there are keys existing in one and not the other"""
|
| 42 |
|
|
all_keys = set(dict1.keys()).union(dict2.keys())
|
| 43 |
|
|
|
| 44 |
|
|
with open(output_file, mode='w', encoding='utf-8', newline='') as csvfile:
|
| 45 |
|
|
writer = csv.writer(csvfile)
|
| 46 |
|
|
writer.writerow(['Key', 'Value in Dict1', 'Value in Dict2', 'Difference Type'])
|
| 47 |
|
|
|
| 48 |
|
|
|
| 49 |
|
|
for key in sorted(all_keys):
|
| 50 |
|
|
val1 = dict1.get(key)
|
| 51 |
|
|
val2 = dict2.get(key)
|
| 52 |
|
|
|
| 53 |
|
|
|
| 54 |
|
|
if val1:
|
| 55 |
|
|
testVal1 = val1.upper()
|
| 56 |
|
|
testVal1 = testVal1.replace(" ","")
|
| 57 |
|
|
testVal1 = testVal1.replace("\n","")
|
| 58 |
|
|
else:
|
| 59 |
|
|
testVal1 = val1
|
| 60 |
|
|
if val2:
|
| 61 |
|
|
testVal2 = val2.upper()
|
| 62 |
|
|
testVal2 = testVal2.replace(" ","")
|
| 63 |
|
|
testVal2 = testVal2.replace("\n","")
|
| 64 |
|
|
else:
|
| 65 |
|
|
testVal2 = val2
|
| 66 |
|
|
|
| 67 |
|
|
if key not in dict1:
|
| 68 |
|
|
writer.writerow([key,'',val2,'Only in Dict2'])
|
| 69 |
|
|
elif key not in dict2:
|
| 70 |
|
|
writer.writerow([key,val1,'','Only in Dict1'])
|
| 71 |
|
|
elif testVal1 != testVal2:
|
| 72 |
|
|
writer.writerow([key,val1,val2,'Different Values'])
|
| 73 |
|
|
else:
|
| 74 |
|
|
## Here there is a match so we just pass.
|
| 75 |
|
|
continue
|
| 76 |
|
|
|
| 77 |
|
|
if __name__ == '__main__':
|
| 78 |
|
|
outputFileName = r"C:\Test_Dir\ATT\20250721_CompareReq\compareReport.csv"
|
| 79 |
|
|
fileNameNew = r"C:\Test_Dir\ATT\20250721_CompareReq\Shiny-3L Feedback-All_20250721.csv"
|
| 80 |
|
|
fileNameOld = r"C:\Test_Dir\ATT\20250721_CompareReq\Shiny-3L Feedback-Older.csv"
|
| 81 |
|
|
|
| 82 |
|
|
newMatrix = csv_to_dict(fileNameNew)
|
| 83 |
|
|
oldMatrix = csv_to_dict(fileNameOld)
|
| 84 |
|
|
|
| 85 |
|
|
key_field_verify(newMatrix)
|
| 86 |
|
|
key_field_verify(oldMatrix)
|
| 87 |
|
|
|
| 88 |
|
|
print(len(newMatrix.keys()))
|
| 89 |
|
|
print(len(oldMatrix.keys()))
|
| 90 |
|
|
|
| 91 |
|
|
compare_dicts(newMatrix,oldMatrix,outputFileName)
|
| 92 |
|
|
|