python learning-109- merging of multiple CSV files

 

Preface

   This article is an operation on the merging of CSV files. Applicable scenarios: multiple employees and students' data are submitted, and then a summary is made. It is too troublesome to use Excel to copy and paste, so I use python's pandas to code The writing includes the use of try except for simple exception handling, error message prompts, etc. The procedure is divided into two:

(1): Source code execution

(2): implemented by the graphical interface

1. Source code execution

#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. Graphical interface GUI execution

#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编程---

3.python package exe file

1) Install two packages     

pip install pywin32
pip install PyInstaller

2) Command

pyinstaller -F -i ico图片路径 py文件路径
#如下:
pyinstaller -F -w -i manage.ico app.py

-F:打包为单文件
-w:Windows程序,不显示命令行窗口
-i:是程序图标
app.py是你要打包的py文件

If there is an error: when using the command, you can remove -w first, and then double-click the exe file, see the error message in the command dos interface, and change it.

For detailed explanation of python packaging, please refer to the following blog:

https://www.cnblogs.com/XJT2018/p/9865239.html

https://www.cnblogs.com/MrRead/p/8393352.html

https://www.cnblogs.com/botoo/p/9644546.html

4. GUI running result display

Guess you like

Origin blog.csdn.net/u013521274/article/details/86374948