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 require loading the CSV into memory, which is obviously unfeasible.
Another approach of using DictWriter() can be used to append a header to the contents of a CSV file. The fieldnames attribute can be used to specify the header of the CSV file and the delimiter argument separates the values by the delimiter given in csv module is needed to carry out the addition of header.
From those guidelines and giving the lack of standardization, the header line is optional in a CSV file. When present, the header line must be the first line in the file and must contain the same number of fields as the records. Header and records lines must use the same field delimiters.
You will need to rewrite the whole file. Simplest is not to use python
echo 'col1, col2, col2,... ' > out.csv
cat in.csv >> out.csv
Python based solutions will work at much higher levels and will be a lot slower. 18GB is a lot of data after all. Better to work with operating system functionality, which will be the fastest.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With