#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,filename,sub) buf = [] if len(buf) != 0: sub = mkSubFile(buf,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))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。