I am working on a simple Tkinter GUI program that displays the different rows between 2 CSV files. I want the header included in "diff.csv"
diff.csv output right now:
hits_data,,,2021-03-23T16:48:50.299Z,2021-03-23T16:48:50.200Z,43.118419647,-77.613418579,176.10,,2,00,461.837500,461.837662,0.000,-50.1,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:48:48.914Z,2021-03-23T16:48:48.800Z,43.118419647,-77.613418579,176.00,,2,00,463.612500,463.612390,0.000,-72.6,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:48:54.534Z,2021-03-23T16:48:54.500Z,43.118415833,-77.613418579,175.80,,2,00,445.500000,445.499915,0.000,-73.9,0.0,,,5777000.0,1,1,,0,,
hits_data,,,2021-03-23T16:49:13.758Z,2021-03-23T16:49:13.700Z,43.118412018,-77.613410950,174.70,,2,00,395.593750,395.595406,0.000,-81.8,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:49:00.960Z,2021-03-23T16:49:00.900Z,43.118412019,-77.613418579,175.70,,2,00,463.300000,463.299637,0.000,-68.3,0.0,,,212000.0,1,1,,0,,
I need help finding a way for this process to write the header of csv1 or csv2 (files will have the same header).
'diff.csv' wanted output:
row_type,mission_id,system_id,sys_date_time,gps_date_time,lat_dd,lon_dd,alt_m,gps_speed_mph,gps_status,trans_uid,freq_cluster_mhz,freq_raw_mhz,bandwidth_khz,rssi_dbm,snr_dbm,modulation,type,duration_s,hit_count,slot_id,slot_status,attention,attention_group,attention_name,call_id
hits_data,,,2021-03-23T16:48:50.299Z,2021-03-23T16:48:50.200Z,43.118419647,-77.613418579,176.10,,2,00,461.837500,461.837662,0.000,-50.1,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:48:48.914Z,2021-03-23T16:48:48.800Z,43.118419647,-77.613418579,176.00,,2,00,463.612500,463.612390,0.000,-72.6,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:48:54.534Z,2021-03-23T16:48:54.500Z,43.118415833,-77.613418579,175.80,,2,00,445.500000,445.499915,0.000,-73.9,0.0,,,5777000.0,1,1,,0,,
hits_data,,,2021-03-23T16:49:13.758Z,2021-03-23T16:49:13.700Z,43.118412018,-77.613410950,174.70,,2,00,395.593750,395.595406,0.000,-81.8,0.0,,,0.0,1,1,,0,,
hits_data,,,2021-03-23T16:49:00.960Z,2021-03-23T16:49:00.900Z,43.118412019,-77.613418579,175.70,,2,00,463.300000,463.299637,0.000,-68.3,0.0,,,212000.0,1,1,,0,,
Likely where the problem is:
Reads 'csv1.csv' and 'csv2.csv' then writes the rows that are different into 'diff.csv'. I found this specific code here:
https://www.edureka.co/community/53649/python-script-to-compare-2-files
def compare1():
with open('csv1.csv', 'r') as file1:
with open('csv2.csv', 'r') as file2:
difference = set(file1).difference(file2)
with open('diff.csv', 'w') as file_out:
for row in difference:
file_out.write(row)
Full code here:
from tkinter import filedialog
from tkinter import *
import shutil
import pandas as pd
from collections import defaultdict
import csv
def gui():
def button_browse_callback():
filename = filedialog.askopenfilename()
src = filename
dst = "csv1.csv"
shutil.copy(src, dst)
entry.delete(0, END)
entry.insert(0, filename)
def button_browse_callback2():
filename2 = filedialog.askopenfilename()
src = filename2
dst = "csv2.csv"
shutil.copy(src, dst)
entry2.delete(0, END)
entry2.insert(0, filename2)
def compare1():
with open('csv1.csv', 'r') as file1:
with open('csv2.csv', 'r') as file2:
difference = set(file1).difference(file2)
with open('diff.csv', 'w') as file_out:
for row in difference:
file_out.write(row)
def parse1():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[0:, 0].values
write(ftCol)
def parse2():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 4].values
write(ftCol)
def parse3():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 5].values
write(ftCol)
def parse4():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 6].values
write(ftCol)
def parse5():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 7].values
write(ftCol)
def parse6():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 11].values
write(ftCol)
def parse7():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 12].values
write(ftCol)
def parse8():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 14].values
write(ftCol)
def parse9():
dataset = pd.read_csv('diff.csv', header=None)
ftCol = dataset.iloc[:, 19].values
write(ftCol)
def write(*message, end = "\n", sep = " "):
text = ""
for item in message:
text += "{}".format(item)
text += sep
text += end
Console.insert(INSERT, text)
def clear():
Console.delete(1.0, END)
root = Tk()
root.title('CSV Compare')
root.configure(background='#3A3B3C',width=10, height=10, padx=6, pady=7)
frame = Frame(root, background='#3A3B3C')
frame.pack()
variable = StringVar(root)
root.resizable(width=False, height=False)
separator = Frame(root, height=2, bd=1, relief=SUNKEN)
statusText = StringVar(root)
statusText.set("Insert CSV files then press Go")
entry = Entry(root, width=110)
message = Label(root, textvariable=statusText, background='lightgrey')
entry2 = Entry(root, width=110)
button_go = Button(root,
text="Go",
command=compare1,
background='orange',
width=10,
height=4)
button_exit = Button(root,
text="Exit",
command=sys.exit,
background='#fed8b1'
)
button_parse = Button(root,
text='System Date',
background='orange',
command=parse1)
button_parse2 = Button(root,
text='GPS Date',
background='orange',
command=parse2)
button_parse3 = Button(root,
text='Latitude',
background='orange',
command=parse3)
button_parse4 = Button(root,
text='Longitude',
background='orange',
command=parse4)
button_parse5 = Button(root,
text='alt_m',
background='orange',
command=parse5)
button_parse6 = Button(root,
text='Freq Cluster',
background='orange',
command=parse6)
button_parse7 = Button(root,
text='Freq Raw',
background='orange',
command=parse7)
button_parse8 = Button(root,
text='rssi_dbm',
background='orange',
command=parse8)
button_parse9 = Button(root,
text='Hit count',
background='orange',
command=parse9)
button_clear = Button(root,
text='Clear',
background='lightblue',
command=clear)
b1 = Button(root,
text="First CSV",
command=button_browse_callback,
background='#fed8b1',)
b2 = Button(root,
text="Second CSV",
command=button_browse_callback2,
background='#fed8b1')
separator2 = Frame(root, height=2, bd=1, relief=SUNKEN)
separator3 = Frame(root, height=2, bd=1, relief=SUNKEN)
statusText2 = StringVar(root)
message2 = Label(root, textvariable=statusText2, background='lightgrey')
Console = Text(root, height=15, width=60)
message.pack()
separator.pack(fill=X, padx=110, pady=5)
b1.pack(side=TOP, anchor=NW, padx=20, pady= 10)
entry.pack(side=TOP, anchor=NW,padx=20)
b2.pack(side=TOP, anchor=NW,padx=20, pady= 10)
entry2.pack(side=TOP, anchor=NW,padx=20)
button_go.pack(pady=6)
b2.pack()
statusText2.set("Extract Data from diff.csv")
separator3.pack(fill=X, padx=90, pady=5)
message2.pack()
separator2.pack(fill=X, padx=90, pady=5)
Console.pack()
variable.set("Data Type")
button_clear.pack(pady=4, padx=5)
button_parse.pack(pady=4, side=LEFT, padx=5)
button_parse2.pack(pady=4,side=LEFT, padx=5)
button_parse3.pack(pady=4,side=LEFT, padx=5 )
button_parse4.pack(pady=4,side=LEFT, padx=5 )
button_parse5.pack(pady=4,side=LEFT, padx=5 )
button_parse6.pack(pady=4,side=LEFT, padx=5 )
button_parse7.pack(pady=4,side=LEFT, padx=5 )
button_parse8.pack(pady=4, side=LEFT, padx=5)
button_parse9.pack(pady=4, side=LEFT, padx=5)
button_exit.pack(side=LEFT, padx=15)
root.maxsize(800, 670);
root.minsize(800, 670);
root.mainloop()
gui()
