将标题添加到CSV而不加载CSV [英] Add header to CSV without loading CSV

查看:130
本文介绍了将标题添加到CSV而不加载CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法添加一个标题行到CSV,而无需将CSV加载到内存中的python?我有一个18GB的CSV我想添加一个标题,所有的方法,我见过要求将CSV加载到内存,这显然是不可行的。

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.

推荐答案

只需使用 csv 模块对行进行迭代,因此它从不会将整个文件加载到内存中

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)

使用 writerows 确保非常好的速度。记忆在这里幸免。一切都是逐行完成的。由于数据已正确处理,您甚至可以更改输出文件中的分隔符和/或引号。

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.

这篇关于将标题添加到CSV而不加载CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆