gpt4 book ai didi

python - 在csv文件python中每第五行插入空白行

转载 作者:行者123 更新时间:2023-12-02 15:21:36 25 4
gpt4 key购买 nike

正如我的标题所说,我想在我的 csv 文件中每隔五行插入一个空行。问题是我的代码在每一行中插入了 5 个空行。其他解决方案,如 this在 stackoverflow 上并没有解决我的问题。

What i want:
1 text on row
2 text on row
3 text on row
4 text on row
5 blank

What i get:
1


text on row
2


text on row
etc.

我的代码是这样的:

with open("new.csv", 'r') as infile:
readie=csv.reader(infile, delimiter=',')
with open("output.csv", 'wt') as output:
outwriter=csv.writer(output, delimiter=',')
sum = 0
i = 0
for row in readie:
sum == row
if sum % 5 == 0:
row_plus = (row, '\n')
outwriter.writerow(row_plus)

最佳答案

你可以尝试这样的事情:

with open("new.csv", 'r') as infile:
readie=csv.reader(infile, delimiter=',')
with open("output.csv", 'wt') as output:
outwriter=csv.writer(output, delimiter=',')
i = 0
for row in readie:
outwriter.writerow(row)
i += 1
if i % 5 == 0:
# write the empty row
outwriter.writerow([])

关于python - 在csv文件python中每第五行插入空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35944238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com