如何使用Python将替代的空白行插入表中? [英] How can I insert alternate blank lines into a table with Python?

查看:86
本文介绍了如何使用Python将替代的空白行插入表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单(但很大)的CSV表,我需要在该表的每行/行之后插入一个空行(=空白).为了以另一种方式进行解释,我希望表的第二行都为空白(但不要删除/覆盖任何原始行).我已经尝试了很多方法,但这是我能想到的最好的方法:

I have a simple (but huge) CSV table, and I need to insert a free (=blank) line/row after each line/row of this table. To explain it in another way, I want every second line of my table to be blank (but without deleting/overwriting any of the original lines). I have tried a lot of ways, but this is the best I could come up with:

with open(sys.argv[1], 'r') as input:
   readie=csv.reader(input, delimiter=',')
   with open("output.csv", 'wt', newline='') as output:
       outwriter=csv.writer(output, delimiter=',')
       for row in readie:
           row_plus = (row, \n)
           outwriter.writerow(row_plus)

它似乎不起作用,因为它会将表中的所有列都塞进一列,并将(row, \n)解释为仅两列.它也只打印"\ n",并且不识别我要它插入另一个换行符.

It doesn't seem to work because it crams all the columns in the table into one column and interprets (row, \n) as two columns only. It also just prints "\n" and doesn't recognize that I want it to insert another line break.

推荐答案

怎么样:

  for row in readie:
     outwriter.writerow(row)
     outwriter.writerow([])

似乎可以做您想做的事.

Seems to do what you want.

这篇关于如何使用Python将替代的空白行插入表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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