我需要从 Excel 读取 CSV 文件,其行可以是任意长度。
问题是 python 保留了这些空白条目,但需要删除它们以供将来的算法使用。下面是输出,我不需要空白条目。
['5', '1', '5', '10', '4', '']
['3', '1', '5', '10', '2', '']
['6', '1', '5', '10', '5', '2']
['9', '10', '5', '10', '7', '']
['8', '5', '5', '10', '7', '']
['1', '1', '5', '10', '', '']
['2', '1', '5', '10', '1', '']
['7', '1', '5', '10', '6', '4']
['4', '1', '5', '10', '3', '1']
最佳答案
这是与 csv
库集成的列表理解:
import csv
with open('input.csv') as in_file:
reader = csv.reader(in_file)
result = [[item for item in row if item != ''] for row in reader]
print result
关于python - 删除CSV文件Python中的空白条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127797/