import csv
def bigdata_csv(openpath,savepath):
f = open(openpath)
csv_read = csv.reader(f)
'''
csv.reader(f)返回一个迭代器。迭代器的好处就是可以不用一次性将大量的数据都读进来,而是如果你需要一条,
就给迭代器一个命令让它输出一条。关于迭代器的优点读者可以另行学习。
'''
i = 0
for line in csv_read:
print(line)
# 如果你的数据量很大,需要在循环中逐行写入数据
# writer.writerow(["index","a_name","b_name"]) # 写入列名
# writer.writerows([[0, 1, 3], [1, 2, 3], [2, 3, 4]]) # 写入多行用writerows
with open(savepath, 'a+',newline='') as csvfile: # a+表示以追加模式写入,如果用w会覆盖掉原来的数据。如果没有newline='',则逐行写入的数据相邻行之间会出现一行空白。读者可以自己试一试。
csv_write = csv.writer(csvfile)
if i == 0:
csv_write.writerow(['', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13'])
i = 1
csv_write.writerow(line) # 写入1行用writerow; row_data是你要写入的数据,最好是list类型。
读写大数据量CSV文件
最新推荐文章于 2024-08-19 19:55:55 发布