How can I split a large file csv file (7GB) in Python

前端 未结 5 1708
我在风中等你
我在风中等你 2020-12-23 22:34

I have a 7GB csv file which I\'d like to split into smaller chunks, so it is readable and faster for analysis in Python on a notebook. I would like to grab a sm

5条回答
  •  离开以前
    2020-12-23 23:09

    Maybe something like this?

    #!/usr/local/cpython-3.3/bin/python
    
    import csv
    
    divisor = 10
    
    outfileno = 1
    outfile = None
    
    with open('big.csv', 'r') as infile:
        for index, row in enumerate(csv.reader(infile)):
            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(row)
    

提交回复
热议问题