-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileCompare
202 lines (73 loc) · 3.44 KB
/
FileCompare
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# -*- coding: utf-8 -*-
import os
import datacompy
import pandas as pd
ftype='sas'
file1='somepath1'
file2='somepath2'
'''IF EXCEL FILE FILL OUT BELOW'''
s1='Sheet1' #(sheet being compared)
s2='Sheet1' #(sheet being compared)
#-------------------------------------COMPARES VIA SET
def csv_file_comp(f1,f2,filetype):
if filetype.lower()=='csv':
df1=pd.read_csv(f1)
df2=pd.read_csv(f2)
diff1=df1[df1!=df2]
diff2=df2[df2!=df1]
if list(df1.columns.values)==list(df2.columns.values):
diff1.to_csv(os.path.join(os.getcwd(),r'FileDifferences1.csv'),index=None, header=df1.columns.values)
diff2.to_csv(os.path.join(os.getcwd(), r'FileDifferences2.csv'),index=None, header=df1.columns.values)
print("Sucessfully compared files")
else: print("Enter CSV to compare files (case insensitive)")
def excel_file_comp(f1,f2,filetype,s1,s2):
if filetype.lower()=='excel' or 'xlsx':
df1=pd.read_excel(f1,sheet_name=s1)
df2=pd.read_excel(f2,sheet_name=s2)
diff1=df1[df1!=df2]
diff2=df2[df2!=df1]
if list(df1.columns.values)==list(df2.columns.values):
diff1.to_excel(os.path.join(os.getcwd(), r'FileDifferences1.xlsx'), header=df1.columns.values)
diff2.to_excel(os.path.join(os.getcwd(), r'FileDifferences2.xlsx'), header=df1.columns.values)
print("Sucessfully compared files")
else: print("Enter Excel or Xlsx to compare files (case insensitive)")
def sas7bdat_file_comp(f1,f2,filetype):
if filetype.lower()=='sas7bdat' or 'sas':
df1=pd.read_sas(f1)
df2=pd.read_sas(f2)
diff1=df1[df1!=df2]
diff2=df2[df2!=df1]
if list(df1.columns.values)==list(df2.columns.values):
diff1.to_csv(os.path.join(os.getcwd(),r'FileDifferences1.csv'),index=None, header=df1.columns.values)
diff2.to_csv(os.path.join(os.getcwd(), r'FileDifferences2.csv'),index=None, header=df1.columns.values)
print("Sucessfully compared files")
else: print("Enter sas to compare files (case insensitive)")
##------------------------------WRITES A SUMMARY COMPARISON REPORT
def DataCompPyReporter(f1,f2,filetype): #If File Type is Excel Enter s1,s2 as parameters
if filetype.lower()=='csv':
df1=pd.read_csv(f1)
df2=pd.read_csv(f2)
if list(df1.columns.values)==list(df2.columns.values):
compare=datacompy.Compare(df1,df2,join_columns=list(df1.columns.values))
print(compare.report())
print(compare.report())
elif filetype.lower()=='sas7bdat' or 'sas':
df1=pd.read_sas(f1)
df2=pd.read_sas(f2)
if list(df1.columns.values)==list(df2.columns.values):
compare=datacompy.Compare(df1,df2,join_columns=list(df1.columns.values))
print(compare.report())
def main():
if ftype =='csv':
csv_file_comp(file1,file2,ftype)
DataCompPyReporter(file1,file2,ftype)
elif ftype == 'sas':
sas7bdat_file_comp(file1,file2,ftype)
DataCompPyReporter(file1,file2,ftype)
elif ftype =='xlsx':
excel_file_comp(file1,file2,ftype,s1,s2) #add sheets as needed
DataCompPyReporter(file1,file2,ftype)
else:
print("Please enter a filetype")
if __name__ == '__main__':
main()