import csv
output = []
with open('test1.csv', 'r') as f:
content = csv.reader(f, delimiter=',')
for line in content:
clean_line = filter(None, line) # remove extra spaces
output.append([[int(i)] for i in clean_line])
>>> print output
[[[11], [10], [8], [12], [13], [11]], [[0], [1], [0], [2], [3], [0]], [[5], [15], [13], [11], [18], [18]]]
测试结果与预期输出相符:
desired = [ [[11],[10],[8],[12],[13],[11]],
[[0],[1],[0],[2],[3],[0]],
[[5],[15],[13],[11],[18],[18]] ]
# print output == desired # True