正如我的标题所说我想在每隔五行在我的csv文件中插入空白行。问题是我的代码在每一行上插入5个空白行。 stackoverflow上的其他解决方案如this并不能解决我的问题。
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)
答案 0 :(得分:2)
您可以尝试这样的事情:
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([])