如何使用TKinter选择两个Excel文件并将它们保存为文件?

huangapple go评论56阅读模式
英文:

How do I use TKinter to select two excel files as keep them as files?

问题

我正在使用一些现成的TKInter代码来选择两个不同的Excel/csv文件:

import tkinter as tk
import tkinter.filedialog as fd

root = tk.Tk()
file_1 = fd.askopenfilenames(parent=root, title='选择文件')
file_2 = fd.askopenfilenames(parent=root, title='选择文件')

我想要做的是选择两个csv/Excel文件,然后使用以下代码进行比较:

import pandas as pd

def cleanup():
    file_1 = pd.read_excel(excel_file, sheet_name="***")
    file_2 = pd.read_excel(excel_file, sheet_name="***")

    df = pd.merge(
        left=file_1,
        right=file_2,
        left_on="User Name",
        right_on="ExternalID",
        how="right")
    matched = df.to_csv("matched.csv", index=False)
    return matched

我遇到的错误是:"TypeError: 只能合并Series或DataFrame对象,传递了<class 'tuple'>。" 如何使用TkInter选择文件并将其视为从excel/csv转换的DataFrame?

英文:

I am using some stock TKInter code to select two separate Excel/csv files:

import tkinter as tk
import tkinter.filedialog as fd

root = tk.Tk()
file_1 = fd.askopenfilenames(parent=root, title=&#39;Choose a file&#39;)
file_2 = fd.askopenfilenames(parent=root, title=&#39;Choose a file&#39;)

What I want to do is to select two csv/Excel files and then compare them using the following code:

import pandas as pd

def cleanup():

    file_1 = pd.read_excel(excel_file, sheet_name=&quot;***&quot;)
    file_2 = pd.read_excel(excel_file, sheet_name=&quot;***&quot;)

    df = pd.merge(
    left=file_1,
    right=file_2,
    left_on=&quot;User Name&quot;,
    right_on = &quot;ExternalID&quot;,
    how = &quot;right&quot;)
    matched = df.to_csv(&quot;matched.csv&quot;, index=False)
    return matched

The error I get is the following: "TypeError: Can only merge Series or DataFrame objects, a <class 'tuple'> was passed." How can I use TkInter to select the file and treat it as a Dataframe converted from excel/csv?

答案1

得分: 1

fd.askopenfilenames 返回一个元组,因为用户可以在一个对话框中选择多个文件。

# 注意要提取单个路径的尾逗号。
file_1, = fd.askopenfilenames(parent=root, title='选择文件')
file_2, = fd.askopenfilenames(parent=root, title='选择文件')

# 或者使用fd.askopenfilename(注意单数的“name”)。
file_1 = fd.askopenfilename(parent=root, title='选择文件')
file_2 = fd.askopenfilename(parent=root, title='选择文件')

excel_1 = pd.read_excel(file_1, sheet_name="***")
excel_2 = pd.read_excel(file_2, sheet_name="***")

df = pd.merge(left=excel_1, right=excel_2, ...)

还要注意,您的cleanup方法正在两次解析相同的文件:

file_1 = pd.read_excel(excel_file, sheet_name="***")
file_2 = pd.read_excel(excel_file, sheet_name="***")
#                      ^^^^^^^^^^
英文:

fd.askopenfilenames returns a tuple of file names, since users can select multiple files in one dialog.

# Note the trailing comma to extract the a single path.
file_1, = fd.askopenfilenames(parent=root, title=&#39;Choose a file&#39;)
file_2, = fd.askopenfilenames(parent=root, title=&#39;Choose a file&#39;)

# Or use fd.askopenfilename (note the singular &quot;name&quot;).
file_1 = fd.askopenfilename(parent=root, title=&#39;Choose a file&#39;)
file_2 = fd.askopenfilename(parent=root, title=&#39;Choose a file&#39;)

excel_1 = pd.read_excel(file_1, sheet_name=&quot;***&quot;)
excel_2 = pd.read_excel(file_2, sheet_name=&quot;***&quot;)

df = pd.merge(left=excel_1, right=excel_2, ...)

Also note that your cleanup method is parsing the same file twice:

file_1 = pd.read_excel(excel_file, sheet_name=&quot;***&quot;)
file_2 = pd.read_excel(excel_file, sheet_name=&quot;***&quot;)
#                      ^^^^^^^^^^

答案2

得分: 1

BoppreH深入核心问题;文件对话框返回一个文件名元组,而不是文件本身。

如果您的文件在同一个文件夹中,另一个选项是使用此代码并从同一个对话框中选择两个文件。无论您先点击哪一个,它都将成为file_1

import tkinter as tk
import tkinter.filedialog as fd
import pandas as pd

root = tk.Tk()
file_names = fd.askopenfilenames(parent=root, title='选择两个文件')

def cleanup():

    file_1 = pd.read_excel(file_names[0], sheet_name="***")
    file_2 = pd.read_excel(file_names[1], sheet_name="***")

    df = pd.merge(
        left=file_1,
        right=file_2,
        left_on="User Name",
        right_on="ExternalID",
        how="right")
    matched = df.to_csv("matched.csv", index=False)
    return matched

print(cleanup())
英文:

BoppreH gets to the heart of the issue; the file dialog returns a tuple of file names, not the files themselves.

If your files are in the same folder, another option is to use this code and select both files from the same dialog. Whichever you click first will become file_1.

import tkinter as tk
import tkinter.filedialog as fd
import pandas as pd

root = tk.Tk()
file_names = fd.askopenfilenames(parent=root, title=&#39;Choose two files&#39;)

def cleanup():

    file_1 = pd.read_excel(file_names[0], sheet_name=&quot;***&quot;)
    file_2 = pd.read_excel(file_names[1], sheet_name=&quot;***&quot;)

    df = pd.merge(
        left=file_1,
        right=file_2,
        left_on=&quot;User Name&quot;,
        right_on = &quot;ExternalID&quot;,
        how = &quot;right&quot;)
    matched = df.to_csv(&quot;matched.csv&quot;, index=False)
    return matched

print(cleanup())

huangapple
  • 本文由 发表于 2023年7月18日 00:18:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706368.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定