【问题标题】:CSV Difference finder not including the header in output fileCSV 差异查找器不包括输出文件中的标题
【发布时间】:2021-07-01 17:04:21
【问题描述】:

我正在开发一个简单的 Tkinter GUI 程序,它显示 2 个 CSV 文件之间的不同行。我希望标题包含在“diff.csv”中

diff.csv 现在输出:

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,,

我需要帮助找到一种方法来让此过程写入 csv1 或 csv2 的标头(文件将具有相同的标头)。

'diff.csv' 想要输出:

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,,

问题可能出在哪里:

读取“csv1.csv”和“csv2.csv”,然后将不同的行写入“diff.csv”。我在这里找到了这个特定的代码:

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)

完整代码在这里:

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()

GUI空白图片:

【问题讨论】:

    标签: python pandas csv tkinter


    【解决方案1】:

    正如我在您的代码中看到的,您只将差异写入输出文件。您应该尝试在 compare1() 函数中的 for 语句之前添加这一行:

    file_out.write(file1.readline())
    

    【讨论】:

    • 这看起来是正确的答案,但我似乎无法让它工作我试过h = file1.readline(1) file_out.write(h) 但这个想法是 100% 正确的,我需要取 csv1 的第一行(标题)并将其写入diff.csv 首先。
    • 谢谢你,经过一夜好眠,我想通了。只需在“file_out.write(file1.readline())”之前添加:“file1.seek(0)”现在可以完美运行
    • 一旦我获得足够的声誉,我会回来支持它:D
    猜你喜欢
    • 2016-01-04
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-14
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多