包含以下内容(注意第一行没有前导空格):
Test1@bigfoot.com
Test11@bigfoot.com
Test1111@bigfoot.com
Test111ew@bigfoot.com
Test12312@bigfoot.com
Test1231321@bigfoot.com
Test1342321@bigfoot.com
....
481 total rows
下面的代码正确地删除了前导空格,但在每个字符串行之后插入一个空行,并且,在每次执行总列表时,都会以随机数目的行数截断。在
^{pr2}$以及:
with open('list.csv') as infile:
reader = csv.DictReader(infile)
fieldnames = reader.fieldnames
for row in reader:
row.update({fieldname: value.strip() for (fieldname, value) in row.items()})
当第一行中的字段名是空的时,它就是假定的。在
这里有几个问题:
newline=""
以写模式打开,否则它会在windows上插入空白strip
而使用lstrip
,否则它会删除行末尾的换行符。会混淆csv阅读器with
上下文块,这样可以确保文件在退出块时是关闭的(最后应该处理随机丢失的行)我的建议:
相关问题 更多 >
编程相关推荐