我正在尝试使用python获取csv文件每一行中的最后一个值的列表。这些行都有五个单元格,但不一致地被填充。有些有一个,两个,三个或四个单元格,其余的单元格都空着。这是csv文件的摘录:
art and entertainment,books and literature,,,
art and entertainment,celebrity fan and gossip,,,
art and entertainment,comics and animation,anime and manga,,
art and entertainment,comics and animation,cartoons,,
art and entertainment,comics and animation,comics,,
art and entertainment,dance,ballet,,
art and entertainment,dance,ballroom dance,,
art and entertainment,dance,belly dance,,
art and entertainment,dance,modern dance,,
art and entertainment,dance,pole dancing,,
art and entertainment,dance,,,
art and entertainment,humor,,,
art and entertainment,movies,film festivals and awards,,
...
和所需的输出:
books and literature, celebrity fan and gossip, anime and manga, cartoons, comics, ballet, ...
尝试以下('z.csv')是您的数据
with open('z.csv') as f:
lines = [l.strip() for l in f.readlines()]
for line in lines:
fields = line.split(',')
last_non_empty = [f for f in fields if f][-1]
print(last_non_empty)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句