• Python将大的csv文件拆分多个小的csv文件


    #ecoding=utf-8
    import os
    import time
    # 2019/9/8 将大的csv文件拆分多个小的csv文件
    
    def mkSubFile(lines, head, srcName, sub):
        [des_filename, extname] = os.path.splitext(srcName)
        filename = des_filename + '_' + str(sub) + extname
        print('make file: %s' % filename)
        fout = open(filename, 'w')
        try:
            fout.writelines([head])
            fout.writelines(lines)
            return sub + 1
        finally:
            fout.close()
    
    
    def splitByLineCount(filename, count):
        fin = open(filename,encoding="utf-8")
        try:
            head = fin.readline()
            buf = []
            sub = 1
            for line in fin:
                buf.append(line)
                if len(buf) == count:
                    sub = mkSubFile(buf, head, filename, sub)
                    buf = []
            if len(buf) != 0:
                sub = mkSubFile(buf, head, filename, sub)
        finally:
            fin.close()
    
    
    if __name__ == '__main__':
        begin = time.time()
        splitByLineCount('training-inspur.csv', 1000)#每个小的csv文件存放1000条
        end = time.time()
        print('time is %d seconds ' % (end - begin))

  • 相关阅读:
    宝塔相关问题
    免费xshell下载
    服务器断电mysql无法恢复
    mysql相关知识
    svn
    tortoisesvn下载 和svn 安装
    nginx+lua乐观锁实现秒杀
    c# asp.net 生成唯一订单号
    c# 关闭软件 进程 杀死进程
    国内开源软件镜像地址搜集
  • 原文地址:https://www.cnblogs.com/zyt-bg/p/11486993.html
Copyright © 2020-2023  润新知