python学习-109-多个CSV文件的合并
前言
本片文章是对CSV文件的合并进行操作,适用场景:多个职工和学生的数据进行提交,然后要做一个汇总,使用Excel进行复制粘贴实在是太麻烦了,所以就使用python的pandas进行代码的编写,包括了使用try except进行简单的异常处理,错误信息提示等。程序分为两个:
(1):源代码执行的
(2):图形界面执行的
1.源代码执行
#coding:utf-8
import pandas as pd
import os
import datetime
def csv_fileMarge(inputfilePath, outputfile):
'''
执行csv utf-8格式的合并
:param inputfilePath: 文件目录
:param outputfile: 最终文件目录与名字
:return:
'''
informationLog=''
#定义列名
columns = ['姓名', '学校', '学号', '成绩', '四级', '六级', '性别', '985', '211']
dataframe = pd.DataFrame([columns])
dataframe.to_csv(outputfile, encoding='utf-8',index=False, header=0)
#合并文件
rightCount=0
errorCount=0
for file in os.listdir(inputfilePath):
try:
data=pd.read_csv(inputfilePath+'/'+file,encoding='utf-8')
dataframe = pd.DataFrame(data)
dataframe.to_csv(outputfile, mode='a', index=False, encoding='utf-8', header=0)
rightCount+=1
except:
print(file + '文件有误')
errorCount+=1
print('文件合并完成 ',rightCount,'个文件成功',errorCount,'个文件有误')
def gettime():
'''
:return: 当前时间的规范形式
'''
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
return now_time
if __name__ == '__main__':
inputfilePath='F:/csv文件合并'
outputfile='data2/合并文件'+gettime()+'.csv'
csv_fileMarge(inputfilePath,outputfile)
2.图形界面GUI执行
#coding:utf-8
import os
import wx
import pandas as pd
import os
import datetime
contents='运行完成'
#选择文件
def OnOpen(a):
dialog = wx.DirDialog(None, '选择一个文件目录',os.getcwd())
if dialog.ShowModal() == wx.ID_OK:
filename.SetValue(dialog.GetPath())
dialog.Destroy()
def OnSave(a):
filePath=filename.GetValue()
contents.SetValue(filePath)
# contents=contents+'增加'
def gettime():
'''
:return: 当前时间的规范形式
'''
now_time = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
return now_time
def csv_fileMarge(a):
inputfilePath=filename.GetValue()
outputfile='E:/CSV文件合并/合并文件'+gettime()+'.csv'
#定义列名
columns = ['姓名', '学校', '学号', '成绩', '四级', '六级', '性别', '985', '211']
dataframe = pd.DataFrame([columns])
dataframe.to_csv(outputfile, encoding='utf-8',index=False, header=0)
#合并文件
informationLog = ''
rightCount=0
errorCount=0
for file in os.listdir(inputfilePath):
try:
data=pd.read_csv(inputfilePath+'/'+file,encoding='utf-8')
dataframe = pd.DataFrame(data)
dataframe.to_csv(outputfile, mode='a', index=False, encoding='utf-8', header=0)
rightCount+=1
except:
# print(file + '文件有误')
informationLog += file + ' 文件有误, '
errorCount+=1
# print('文件合并完成 ',rightCount,'个文件成功',errorCount,'个文件有误')
informationLog += ' 文件合并完成: ' + str(rightCount) + ' 个文件成功, ' + str(errorCount) + ' 个文件有误 '
contents.SetValue(informationLog)
#------------------------------------------------------------------------------------GUI编程---
#定义程序类对象
app = wx.App()
#创建顶层窗口
win = wx.Frame(None, title='CSV文件合并 结果目录:E:/CSV文件合并 (先把 E:/CSV文件合并 文件夹建立)',size=(600, 400))
#面板
bkg = wx.Panel(win)
loadButton = wx.Button(bkg,label = u'选择文件目录')
saveButton = wx.Button(bkg,label = u'合并')
loadButton.Bind(wx.EVT_BUTTON, OnOpen)
saveButton.Bind(wx.EVT_BUTTON, csv_fileMarge)
#显示文件路径
filename = wx.TextCtrl(bkg)
#显示文章抽取结果
contents = wx.TextCtrl(bkg, style=wx.TE_MULTILINE)
hbox = wx.BoxSizer()
hbox.Add(filename, proportion=1,flag=wx.EXPAND)
hbox.Add(loadButton, proportion=0, flag=wx.LEFT, border=5)
hbox.Add(saveButton, proportion=0, flag=wx.LEFT, border=5)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(hbox, proportion=0, flag=wx.EXPAND | wx.ALL, border=5)
vbox.Add(contents, proportion=1, flag=wx.EXPAND | wx.LEFT | wx.BOTTOM | wx.RIGHT, border=5)
bkg.SetSizer(vbox)
win.Show()
app.MainLoop()
#------------------------------------------------------------------------------------GUI编程---