python3-4.GUI_CSV转换工具_将CSV文件转换为txt.mat文件

from tkinter import *
import numpy as np
import pandas as pd
import scipy.io as scio
import os
from tkinter.scrolledtext import ScrolledText




#转换为txt文件
def write2txt():
    path = filename.get()
    
    #打开csv文件
    df = pd.read_csv(path,header=None,names=['row','col','in_t','Ramp','Off_t'])
    list_data =df.values.tolist()
    
    txtName =os.path.dirname(path)+'\\'+os.path.basename(path)[:-4]+'.txt'
    f = open(txtName, 'w')
    for i in list_data:
        s = str(i).replace('[','').replace(']','')#去除[],这两行按数据不同,可以选择
        s = s.replace("'",'').replace(',','') +'\n'   #去除单引号,逗号,每行末尾追加换行符
        f.write(s)
    f.close()
    var.set('write2txt finish')


#CSV文件转为mat文件
#mat文件读取出来是一个字典,所以要保存为一个字典
def write2mat():
    path = filename.get()
    
    df = pd.read_csv(path,header=None,names=['row','col','in_t','Ramp','Off_t'])
    list_data =df.values.tolist()
    
    txtName =os.path.dirname(path)+'\\'+os.path.basename(path)[:-4]+'.mat'
    scio.savemat(txtName, mdict={'data': list_data})
    var.set('write2tmat finish')

top = Tk()
top.title("CSV tool")
#top.geometry("400x100")

var = StringVar()
var.set('input filepath')
lab = Label(top,textvariable = var)
lab.pack(side=BOTTOM, expand=True, fill=BOTH)


filename = Entry()
filename.pack(side=LEFT, expand=True, fill=X)


Button(text='txt', command=write2txt).pack(side=LEFT)
Button(text='mat', command=write2mat).pack(side=LEFT)

mainloop()
    

结果:点击txt文件,会将CSV转为txt;点击mat,会将CSV转为mat

空白处需要输入绝对路径

python3-4.GUI_CSV转换工具_将CSV文件转换为txt.mat文件_第1张图片

发布可执行程序方法:

1.安装pyinstaller

2.在编写的py文件夹在cmd输入pyinstaller -F xxx.py

最终的结果在dist文件下exe文件;拷贝到别人的电脑也可以用哦

你可能感兴趣的:(python笔记,GUI,CSV,mat,txt)