我有一个制表符分隔的 CSV 文件。它没有任何标题。如何添加标题?这就是我尝试过的。出现内存错误。 我正在使用 Jupyter 和 python 3.6.*
import csv
with open('main_file.csv',newline='') as f:
r = csv.reader(f)
data = [line for line in r]
with open('file.csv','wb',newline='') as f:
w = csv.writer(f, delimiter='\t')
w.writerow(['a', 'b','c','d','e','f'])
w.writerows(data)
我的 csv 数据示例
1 2 3 4
5 6 7 8
9 10 11 12
请您参考如下方法:
这样的事情有帮助吗?
import pandas as pd
my_file = "C:\my_path\my_file.csv"
df = pd.read_csv(my_file, sep='\t', names = ["a", "b", "c", "d", "e", "f"])
df.to_csv(my_file, sep='\t', index = False)
