Update HeaderChecker.py
Browse files- HeaderChecker.py +99 -93
HeaderChecker.py
CHANGED
@@ -1,93 +1,99 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pandas as pd
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
return
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
st.
|
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 |
-
if
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
from io import StringIO
|
4 |
+
|
5 |
+
def load_file(file, header_row):
|
6 |
+
if file is None:
|
7 |
+
return None, None
|
8 |
+
try:
|
9 |
+
if file.name.endswith(".xlsx"):
|
10 |
+
df = pd.read_excel(file, header=header_row - 1)
|
11 |
+
else:
|
12 |
+
df = pd.read_csv(StringIO(file.getvalue().decode("utf-8")), header=header_row - 1)
|
13 |
+
headers = df.columns.tolist()
|
14 |
+
return headers, df
|
15 |
+
except Exception as e:
|
16 |
+
st.error(f"Error loading file: {e}")
|
17 |
+
return None, None
|
18 |
+
|
19 |
+
def main():
|
20 |
+
st.title("File Header and Data Comparison Tool")
|
21 |
+
|
22 |
+
# First file upload
|
23 |
+
st.header("Upload First File")
|
24 |
+
if "file1" not in st.session_state:
|
25 |
+
st.session_state["file1"] = None
|
26 |
+
st.session_state["headers1"] = None
|
27 |
+
st.session_state["df1"] = None
|
28 |
+
|
29 |
+
file1 = st.file_uploader("Choose the first file (CSV or Excel)", type=["csv", "xlsx"], key="file1_uploader")
|
30 |
+
if file1:
|
31 |
+
header_row1 = st.number_input("Specify the row number for headers in the first file", min_value=1, value=1, key="header_row1")
|
32 |
+
if st.button("Load First File", key="load_file1"):
|
33 |
+
headers1, df1 = load_file(file1, header_row1)
|
34 |
+
if headers1:
|
35 |
+
st.session_state["file1"] = file1
|
36 |
+
st.session_state["headers1"] = headers1
|
37 |
+
st.session_state["df1"] = df1
|
38 |
+
st.success(f"Headers from the first file: {headers1}")
|
39 |
+
else:
|
40 |
+
st.error("Failed to load the first file.")
|
41 |
+
|
42 |
+
# Second file upload
|
43 |
+
st.header("Upload Second File")
|
44 |
+
if "file2" not in st.session_state:
|
45 |
+
st.session_state["file2"] = None
|
46 |
+
st.session_state["headers2"] = None
|
47 |
+
st.session_state["df2"] = None
|
48 |
+
|
49 |
+
file2 = st.file_uploader("Choose the second file (CSV or Excel)", type=["csv", "xlsx"], key="file2_uploader")
|
50 |
+
if file2:
|
51 |
+
header_row2 = st.number_input("Specify the row number for headers in the second file", min_value=1, value=1, key="header_row2")
|
52 |
+
if st.button("Load Second File", key="load_file2"):
|
53 |
+
headers2, df2 = load_file(file2, header_row2)
|
54 |
+
if headers2:
|
55 |
+
st.session_state["file2"] = file2
|
56 |
+
st.session_state["headers2"] = headers2
|
57 |
+
st.session_state["df2"] = df2
|
58 |
+
st.success(f"Headers from the second file: {headers2}")
|
59 |
+
else:
|
60 |
+
st.error("Failed to load the second file.")
|
61 |
+
|
62 |
+
# Compare headers
|
63 |
+
if st.session_state["headers1"] and st.session_state["headers2"]:
|
64 |
+
headers1 = st.session_state["headers1"]
|
65 |
+
headers2 = st.session_state["headers2"]
|
66 |
+
|
67 |
+
st.header("Header Comparison Results")
|
68 |
+
missing_in_file2 = [header for header in headers1 if header not in headers2]
|
69 |
+
missing_in_file1 = [header for header in headers2 if header not in headers1]
|
70 |
+
|
71 |
+
if missing_in_file2 or missing_in_file1:
|
72 |
+
st.write("Differences in headers:")
|
73 |
+
if missing_in_file2:
|
74 |
+
st.write(f"Headers in File 1 but not in File 2: {missing_in_file2}")
|
75 |
+
if missing_in_file1:
|
76 |
+
st.write(f"Headers in File 2 but not in File 1: {missing_in_file1}")
|
77 |
+
else:
|
78 |
+
st.success("Headers match perfectly!")
|
79 |
+
|
80 |
+
# Compare column values where headers match
|
81 |
+
common_headers = [header for header in headers1 if header in headers2]
|
82 |
+
if common_headers:
|
83 |
+
st.header("Column Value Differences")
|
84 |
+
df1 = st.session_state["df1"]
|
85 |
+
df2 = st.session_state["df2"]
|
86 |
+
|
87 |
+
for header in common_headers:
|
88 |
+
col1 = df1[header].dropna().unique()
|
89 |
+
col2 = df2[header].dropna().unique()
|
90 |
+
|
91 |
+
if not pd.Series(col1).equals(pd.Series(col2)):
|
92 |
+
st.write(f"Column '{header}' differs between the files.")
|
93 |
+
st.write(f"Values in File 1 but not in File 2: {set(col1) - set(col2)}")
|
94 |
+
st.write(f"Values in File 2 but not in File 1: {set(col2) - set(col1)}")
|
95 |
+
else:
|
96 |
+
st.write(f"Column '{header}' matches in both files.")
|
97 |
+
|
98 |
+
if __name__ == "__main__":
|
99 |
+
main()
|