我正在尝试编写一个简单的python程序(在使用Pydev的Eclipse Juno中)加载多个.CSV文件,以便通过CSV复制和重新输出。 (最终“复制”功能将被替换为选择某些列,然后重新输出csv文件)
到目前为止,我已经获得了上传单个CSV文件的程序并将其输出到用户通过保存对话框指定的位置(现在只复制文件)。
弹出一个对话框并要求选择文件(多个选择工作),但我不确定如何解释/解析并将多个csv输入文件静态保存到我的计算机上的指定位置(C:/)。
这是一个简单的程序任务:
输入CSV文件 - >复制 - >将CSV文件输出到计算机上的静态位置
我将在此帖中包含我的代码,
非常感谢任何帮助!! : - )
文件上传
from Tkinter import Tk
from tkFileDialog import askopenfilenames
Tk().withdraw()
filenames = askopenfilenames()
print(filenames)
文件复制/操作
import csv
import tkFileDialog
ifile = open(filenames)
reader = csv.reader(ifile)
另存为对话框
ofile = tkFileDialog.asksaveasfilename()
print filenames
writer = csv.writer(open(ofile, 'wb'))
for row in reader:
writer.writerow(row)
状态对话框
import tkMessageBox
tkMessageBox.showinfo(title="Conversion Successful", message="CSV File has been successfully converted")
答案 0 :(得分:0)
如果不确切知道自己要做什么,可以尝试这样的事情:
from Tkinter import Tk
from tkFileDialog import askopenfilenames
Tk().withdraw()
filenames = askopenfilenames()
# askopenfilenames() returns a list of filenames. Loop through them one at a time
for fin in filenames:
ifile = open(fin)
reader = csv.reader(ifile)
# For each file, ask where to save it, then write it out.
ofile = tkFileDialog.asksaveasfilename()
writer = csv.writer(open(ofile, 'wb'))
for row in reader:
# Whatever you modify probably goes here.
writer.writerow(row)
tkMessageBox.showinfo(title="Conversion Successful", message="CSV File '{0}' has been successfully converted and was written to '{1}'".format(fin,ofile)
ifile.close()