英文:
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='Choose a file')
file_2 = fd.askopenfilenames(parent=root, title='Choose a file')
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="***")
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
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='Choose a file')
file_2, = fd.askopenfilenames(parent=root, title='Choose a file')
# Or use fd.askopenfilename (note the singular "name").
file_1 = fd.askopenfilename(parent=root, title='Choose a file')
file_2 = fd.askopenfilename(parent=root, title='Choose a file')
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, ...)
Also note that your cleanup
method is parsing the same file twice:
file_1 = pd.read_excel(excel_file, sheet_name="***")
file_2 = pd.read_excel(excel_file, sheet_name="***")
# ^^^^^^^^^^
答案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='Choose two files')
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())
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论