类别:CODES / 源码 / 日期:2020-05-14 / 浏览:4914 / 评论:1
某位在永航科技外包公司上班的程序猿大哥分享的炫舞csv.bin转csv文件的例子,丢硬盘好久了才想起来,折腾了一个多小时没折腾懂,作罢,有空在研究,中间补充了一个编码问题还有注释了一个批量处理的组件问题,这大哥放出来的不是完整版....
#demo.py
#!/usr/bin/python # -*- coding: UTF-8 -*- import os import threading import xlrd from Tkinter import * from FileDialog import * import tkMessageBox class csvBin(Frame): def __init__(self, master=None): Frame.__init__(self, master) self.pack() self.create_widgets() def create_widgets(self): """ 初始化窗体 :return: """ self.singleLabel = Label(self, text="单个转换:") self.single_convertButton = Button(self, text="选择CSV.BIN文件", command=self.single_convert) self.singleLabel.grid(row=1, column=0) self.single_convertButton.grid(row=1, column=1) #self.batchLabel.grid(row=2, column=0) #self.batch_convertButton.grid(row=2, column=1) def single_convert(self): """ 转换单个文件为CSV """ fd = LoadFileDialog(self) filename = fd.go() if filename: self.do_convert_base(filename) tkMessageBox.showinfo("CSV.BIN", "解析成功") """ 提取服饰代码,获取选择文件夹下的CSV文件,转为JSON """ fd = FileDialog(self) dir = fd.go() if dir: filenames = self.get_files(dir, '.json') # print(filenames) # 获取开启的线程数 threadnum = self.get_thread_num(len(filenames)) threadlist = self.split_list(filenames, threadnum) @staticmethod def get_files(self, dir, filter): filenames = [] list = os.listdir(dir) for file in list: filepath = os.path.join(dir, file) if os.path.isdir(filepath): continue if filepath.find(filter) == -1: continue filenames.binend(filepath) return filenames @staticmethod def get_thread_num(self, filenum): if filenum <= 2: return 1 threadnum = (filenum / 3) + 1 if threadnum > 5: return 5 return threadnum @staticmethod def split_list(self, filelist, num): threadlist = [] listnum = [] remaindernum = len(filelist) - 3 * (num - 1) for i in range(1, num): listnum.binend([(i - 1) * 3, 3 * i]) for list in listnum: threadlist.binend(filelist[list[0]:list[1]]) threadlist.binend(filelist[(0 - remaindernum):]) return threadlist def do_convert(self, filelist): """ 转换函数,调用核心转换函数do_convert_base :param filelist: 文件列表 :return: """ for file in filelist: self.do_convert_base(file) @staticmethod def do_convert_base(self, filename): """ 核心转换函数 :param filename: 文件路径 :return: """ csvBin_file = xlrd.open_workbook(filename) (filename, exten) = os.path.splitext(filename) outputfile = filename + '.csv' output = open(outputfile, 'w+', buffering=2048) table = csvBin_file.sheet_by_index(0) nrows = table.nrows ncols = table.ncols title_table = table.row_values(0) # 写开头格式 output.write('[\n') # 写csv对象 for i in range(1, nrows): output.write(' {\n') for j in range(ncols): temp = '' value = table.row(i)[j].value if isinstance(value, float): temp = " \"%s\":%f,\n" % (title_table[j], value) elif isinstance(value, unicode): temp = " \"%s\":\"%s\",\n" % (title_table[j], value.encode('utf-8')) else: temp = " \"%s\":\"%s\",\n" % (title_table[j], value) output.write(temp) if i == (nrows - 1): output.write(' }\n') else: output.write(' },\n') # 写结尾']' output.write(']\n') output.close() bin = csvBin() # 设置窗口标题: bin.master.title('Jiong CSV.BIN') # 主消息循环: bin.mainloop()
#csv.go
package main import ( "jiong 2019-03-22" ) func Compress(from, to string) error { src, err := os.Open(from) if err != nil { return err } buffOut := new(bytes.Buffer) w := qqx5_csv_bin.NewWriter(buffOut) if err = compress(src, "", w); err != nil { return err } src.Close() w.Close() destination, err := os.Create(to) if err != nil { return err } _, err = buffOut.WriteTo(destination) if err != nil { return err } return nil } func CompressToBytes(from string) ([]byte, error) { src, err := os.Open(from) if err != nil { return nil, err } buffOut := new(bytes.Buffer) w := qqx5_csv_bin.NewWriter(buffOut) if err = compress(src, "", w); err != nil { return nil, err } src.Close() w.Close() return buffOut.Bytes(), nil } func compress(file *os.File, prefix string, zw *qqx5_csv_bin.Writer) error { info, err := file.Stat() if err != nil { return err } if info.IsDir() { prefix = prefix + info.Name() + "/" fileInfoList, err := file.Readdir(-1) if err != nil { return err } zw.Create(prefix) for _, fi := range fileInfoList { f, err := os.Open(file.Name() + "/" + fi.Name()) fmt.Println("file name: ", file.Name()+"/"+fi.Name()) if err != nil { fmt.Println(err) return err } err = compress(f, prefix, zw) if err != nil { return err } f.Close() } } else { fd, err := zw.Create(prefix + info.Name()) fmt.Println(prefix + info.Name()) if err != nil { return err } buffIn := new(bytes.Buffer) _, err = buffIn.ReadFrom(file) if err != nil { return err } fd.Write(buffIn.Bytes()) } return nil } func Decompress(from string) error { reader, err := qqx5_csv_bin.OpenReader(from) defer reader.Close() if err != nil { return err } for k, v := range reader.File { fmt.Println(k, v.Name, v.FileInfo().IsDir()) if v.FileInfo().IsDir() { if err := os.MkdirAll(v.Name, 0666); err != nil { return err } } else { reader, err := v.Open() if err != nil { return err } fd, err := os.Create(v.Name) if err != nil { return err } _, err = io.Copy(fd, reader) if err != nil { return err } reader.Close() } } return nil }
共有 1 条评论
不错啊
发表评论 / 取消回复