2015-12-04 53 views
0

我有以下格式的數據(在一個CSV文件):數據轉換 - Python的

a b c 
b 
a 
a c d 
b c 
b c 

我想上面的數據隱蔽以下格式(列表):

[['a', 'b', 'c'], 
['b'], 
['a'], 
['a', 'c', 'd'], 
['b', 'c'], 
['b', 'c']] 

我迄今所做的:

import csv 

fileName = "toydataset.csv" 

data = open(fileName, 'r') 
reader = csv.reader(data) 
allRows = [row for row in reader] 
allRows 

但是,輸出看起來是這樣的:

​​

如何從列表中刪除這些空值,以便輸出如下所示?

[['a', 'b', 'c'], 
['b'], 
['a'], 
['a', 'c', 'd'], 
['b', 'c'], 
['b', 'c']] 

回答

3
In [8]: l 
Out[8]: 
[['a', 'b', 'c'], 
['b', '', ''], 
['a', '', ''], 
['a', 'c', 'd'], 
['b', 'c', ''], 
['b', 'c', '']] 

In [9]: [ filter(None, a) for a in l] 
Out[9]: [['a', 'b', 'c'], ['b'], ['a'], ['a', 'c', 'd'], ['b', 'c'], ['b', 'c']] 
2

這裏是一個另類。如果你是這樣的事情。

import csv 


def clean_item(item): 
    res = [x for x in item if x] 
    return res 


def main(): 
    # PEP-8! 
    file_name = "toydataset.csv" 
    with open(file_name, 'r') as data: # don't corrupt your data! 
     reader = csv.reader(data) 
     # PEP-8! 
     all_rows = [row for row in reader if row] 
     print(all_rows) 


if __name__ == '__main__': 
    main() 
0

你也可以做到不使用任何內置的方法:

>>> l 
[['a', 'b', 'c'], ['b', '', ''], ['a', '', ''], ['a', 'c', 'd'], ['b', 'c', ''], ['b', 'c', '']] 
>>> [[i for i in j if i] for j in l] 
    [['a', 'b', 'c'], ['b'], ['a'], ['a', 'c', 'd'], ['b', 'c'], ['b', 'c']]