有csv.reader告诉它是在最后一行 [英] Have csv.reader tell when it is on the last line
问题描述
显然,某些csv输出实现在字段为null时,会从最后一行的右侧截断字段分隔符,而在文件中只有最后一行。
Apparently some csv output implementation somewhere truncates field separators from the right on the last row and only the last row in the file when the fields are null.
输入示例csv,fields'c'and'd'are nullable:
Example input csv, fields 'c' and 'd' are nullable:
a|b|c|d
1|2||
1|2|3|4
3|4||
2|3
在下面的脚本中,在最后一行,所以我知道如何正确处理它?</ p>
In something like the script below, how can I tell whether I am on the last line so I know how to handle it appropriately?
import csv
reader = csv.reader(open('somefile.csv'), delimiter='|', quotechar=None)
header = reader.next()
for line_num, row in enumerate(reader):
assert len(row) == len(header)
....
推荐答案
基本上你只知道你已经用完了之后已经用完。所以你可以包装 reader
迭代器,例如。如下:
Basically you only know you've run out after you've run out. So you could wrap the reader
iterator, e.g. as follows:
def isLast(itr):
old = itr.next()
for new in itr:
yield False, old
old = new
yield True, old
$ b (isLast(阅读器))中为line_num,(is_last,row)更改您的代码为:
and change your code to:
for line_num, (is_last, row) in enumerate(isLast(reader)):
if not is_last: assert len(row) == len(header)
这篇关于有csv.reader告诉它是在最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!