如何使用python将大文件拆分为块

时间:2018-06-11 14:53:36

标签: python csv

import csv 

divisor = 1000000 

outfileno = 1 
outfile = None 

with open('o77069882.txt', 'r') as infile: 

    infile_iter = csv.reader(infile) 
    header = next(infile_iter) 
    for index, row in enumerate(infile_iter): 
        if index % divisor == 0: 
            if outfile is not None: 
                outfile.close() 
            outfilename = 'big-{}.csv'.format(outfileno) 
            outfile = open(outfilename, 'w') 
            outfileno += 1 
            writer = csv.writer(outfile) 
            writer.writerow(header) 
        writer.writerow(row) 
    # Don't forget to close the last file 
    if outfile is not None: 
        outfile.close()

我使用上面的代码将我的文件分成大小为100000的块,它完成了工作,但是第一个文件中的每一行都被引号("")括起来,例如&# 34; abc,dfg,ghj,kil" 。创建的第二个和第三个文件没有这个问题,任何人都可以帮我修改我的代码以摆脱上述错误。

0 个答案:

没有答案