Add header to CSV without loading CSV

前端 未结 3 2016
北海茫月
北海茫月 2020-12-11 02:46

Is there a way to add a header row to a CSV without loading the CSV into memory in python? I have an 18GB CSV I want to add a header to, and all the methods I\'ve seen requi

3条回答
  •  情深已故
    2020-12-11 03:14

    Just use the fact that csv module iterates on the rows, so it never loads the whole file in memory

    import csv
    
    with open("huge_csv.csv") as fr, open("huge_output.csv","w",newline='') as fw:
        cr = csv.reader(fr)
        cw = csv.writer(fw)
        cw.writerow(["title1","title2","title3"])
        cw.writerows(cr)
    

    using writerows ensure a very good speed. The memory is spared here. Everything is done line-by-line. Since the data is properly processed, you could even change the separator and/or the quoting in the output file.

提交回复
热议问题