2016-02-16 205 views
1

將'NULL'轉換爲'0'我將下面的結果作爲item_list獲得。但是,我想只有一個列表,而不是item_list的列表列表。我能做什麼?將列表轉換爲一個列表

for f in files: 
    with open(f) as f: 
     f.next() 
     rows = csv.reader(f) 

     for row in rows: 

      item_list = [] 

      for row_item in row: 
       output_string = map(lambda x: '0' if x=='NULL' else x, row_item.split(" ")) 
       item_list.append(output_string) 

      print item_list 

row: ['2015-07-18 07:17:09.000', '345', '138', '123','111','146', '128', '116', 'NULL', 'NULL', '124', '137', '29', '49', '62', '3', '17', '13', 'NULL', 'NULL', '3', '6', '7', '0', '2', '2', 'NULL', 'NULL', '5', '1'] 
['2015-07-18 07:19:09.000', '345', '138', '123', '109', '144', '128', '109', 'NULL', 'NULL', '123', '136', '21', '58', '72', '5', '16', '10', 'NULL', 'NULL', '2', '7', '9', '0', '2', '1', 'NULL', 'NULL', '6', '1'] 
['2015-07-18 07:21:09.000', '345', '129', '116', '110', '144', '123', '114', 'NULL', 'NULL', '118', '133', '28', '58', '69', '3', '26', '8', 'NULL', 'NULL', '3', '7', '8', '0', '3', '1', 'NULL', 'NULL', '6', '1'] 
['2015-07-18 07:23:09.000', '345', '135', '123', '112', '145', '120', '117', 'NULL', 'NULL', '123', '132', '26', '44', '52', '6', '23', '8', 'NULL', 'NULL', '3', '5', '6', '0', '2', '1', 'NULL', 'NULL', '4', '1'] 
['2015-07-18 07:25:09.000', '345', '130', '124', '115', '145', '120', '119', 'NULL', 'NULL', '123', '132', '27', '55', '54', '3', '24', '4', 'NULL', 'NULL', '3', '6', '6', '0', '3', '0', 'NULL', 'NULL', '5', '1'] 
['2015-07-18 07:27:09.000', '345', '127', '124', '118', '145', '118', '122', 'NULL', 'NULL', '123', '131', '27', '53', '49', '3', '21', '6', 'NULL', 'NULL', '3', '6', '6', '0', '3', '0', 'NULL', 'NULL', '5', '1'] 
['2015-07-18 07:29:09.000', '345', '135', '128', '119', '147', '114', '125', 'NULL', 'NULL', '127', '130', '24', '62', '50', '13', '15', '7', 'NULL', 'NULL', '3', '7', '6', '1', '3', '0', 'NULL', 'NULL', '5', '2'] 
['2015-07-18 07:31:09.000', '345', '137', '129', '119', '148', '117', '121', 'NULL', 'NULL', '128', '132', '22', '44', '33', '10', '18', '10', 'NULL', 'NULL', '2', '5', '4', '1', '2', '1', 'NULL', 'NULL', '3', '1'] 
['2015-07-18 07:33:09.000', '345', '134', '124', '116', '151', '122', '122', 'NULL', 'NULL', '124', '136', '24', '53', '57', '8', '26', '8', 'NULL', 'NULL', '3', '6', '7', '1', '3', '1', 'NULL', 'NULL', '5', '2'] 


item_list[]: [['2015-11-17', '08:55:02.000'], ['345'], ['108'], ['106'], ['111'], ['134'], ['114'], ['84'], ['0'], ['0'], ['108'], ['124'], ['52'], ['73'], ['77'], ['15'], ['36'], ['17'] ['0'], ['0'], ['9'], ['11'], ['9'], ['1'], ['4'], ['3'], ['0'], ['0']....] 

回答

2

相反的.append(),使用.extend()

item_list.extend(output_string) 
0

或者你可以使用+操盤的.append()方法,這樣它正好連接:

item_list += output_string 

我希望這是有道理的。