英文:
How can I ask the user for a directory to save a .csv file using the 'save as' window?
问题
我正在编写一个程序,它会从用户选择的输入文件创建一个.csv文件,并将该.csv文件保存到用户选择的位置。
以下是你的代码,保存到与项目相同的目录:
# 创建要导出的二维列表
full_list = list(zip(a_list, b_list, c_list))
# 创建.csv文件
myFile = open('output.csv', 'w', newline='')
writer = csv.writer(myFile)
# 写入.csv文件
for unit in full_list:
writer.writerow(unit)
你想要用户选择输出目录和文件名,使用"另存为"窗口。你可以使用filedialog.asksaveasfilename()
来实现这一点。以下是一种方法,将用户选择的文件路径传递给保存.csv文件:
output_file_path = filedialog.asksaveasfilename(defaultextension=".csv", filetypes=[("CSV Files", "*.csv")])
if output_file_path:
myFile = open(output_file_path, 'w', newline='')
writer = csv.writer(myFile)
for unit in full_list:
writer.writerow(unit)
这样,用户将能够选择输出的目录和文件名,并且你可以使用output_file_path
来保存.csv文件。
英文:
I am writing a program that will create a .csv file from an input file chosen by the user, and save that .csv file to where ever the user pleases.
Here's what I have, which saves to the same directory as the project.
# Create 2d list to export
full_list = list(zip(a_list, b_list, c_list))
# Create .csv
myFile = open('output.csv', 'w', newline='')
writer = csv.writer(myFile)
# Write to .csv
for unit in full_list:
writer.writerow(unit)
How can I have the user select a directory and file name for the output, using the 'save as' window?
I have managed the input part using tkinter filedialog.askopenfilename()
to return the directory of the file to open. I can't get the output part working with filedialog.asksaveasfilename()
. They seem to do the same thing? Where can I feed this file path input so the .csv is saved there?
I have tried:
myFile = open(filedialog.asksaveasfilename(), 'output.csv', 'w', newline='')
writer = csv.writer(myFile)
Everything I find online just explains how to get the 'save as' window open, not how to feed a file in to be saved.
答案1
得分: 0
你可以使用filedialog.asksaveasfilename()
函数来提示用户输入文件名和保存CSV文件的目录。
import csv
from tkinter import filedialog, Tk
# 创建要导出的二维列表
full_list = list(zip(a_list, b_list, c_list))
# 提示用户选择保存CSV文件的目录和文件名
root = Tk()
root.withdraw()
file_path = filedialog.asksaveasfilename(defaultextension='.csv')
# 创建CSV文件并写入数据
with open(file_path, 'w', newline='') as myFile:
writer = csv.writer(myFile)
for unit in full_list:
writer.writerow(unit)
请注意,这段代码用于将数据保存为CSV文件并使用filedialog
库来与用户交互以获取保存路径。
英文:
You can use the filedialog.asksaveasfilename()
function to prompt the user for the file name and directory to save the CSV file
import csv
from tkinter import filedialog, Tk
# Create 2d list to export
full_list = list(zip(a_list, b_list, c_list))
# Prompt the user to choose a directory and file name to save the CSV file
root = Tk()
root.withdraw()
file_path = filedialog.asksaveasfilename(defaultextension='.csv')
# Create the CSV file and write to it
with open(file_path, 'w', newline='') as myFile:
writer = csv.writer(myFile)
for unit in full_list:
writer.writerow(unit)
答案2
得分: 0
myFile = open('output.csv', 'w', newline='')
file_path = filedialog.asksaveasfilename(defaultextension='.csv')
myFile = open(file_path, 'w', newline='')
英文:
myFile = open('output.csv', 'w', newline='')
I thought 'output.csv'
argument was just for the file name, but it can be fed the file path as well.
file_path = filedialog.asksaveasfilename(defaultextension='.csv')
myFile = open(file_path, 'w', newline='')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论