2016-02-15 51 views
-3

我在我的代码中将“NULL”项目转换为0。然后,我必须将它们追加到列表中,并且我想为s1,s2..s8创建一个列表。我想追加'str'对象

我有这个属性错误,而追加:'str'对象没有属性'追加'。如果有人能帮助我,我会很高兴。


for f in files: 

    with open(f) as f: 
     f.next() 
     rows = csv.reader(f) 
     for row in rows: 
      date = row[0] 
      s1 = row[2] 
      s2 = row[3] 
      s3 = row[4] 
      s4 = row[5] 
      s5 = row[6] 
      s6 = row[7] 
      s7 = row[8] 
      s8 = row[9] 

      items = [] 
      for item in row: 
       output_string = map(lambda x: '0' if x=='NULL' else x, item.split(",")) 
       items.append(float(output_string)) 

      print items 

输入:

行:

['2015-07-27 15:26:15.000', '345', '93', '91', '102', '134', '101', '76', 'NULL', 'NULL', '95', '117', '27', '46', '70', '66', '60', '34', 'NULL', 'NULL', '8', '13', '9', '9', '12', '8', 'NULL', 'NULL', '10', '10'] 
['2015-07-27 15:28:15.000', '345', '89', '100', '108', '143', '97', '84', 'NULL', 'NULL', '99', '120', '23', '47', '69', '51', '53', '27', 'NULL', 'NULL', '7', '10', '8', '7', '9', '6', 'NULL', 'NULL', '8', '8'] 
['2015-07-27 15:30:15.000', '345', '89', '109', '109', '137', '96', '84', 'NULL', 'NULL', '102', '116', '26', '49', '56', '57', '54', '30', 'NULL', 'NULL', '6', '8', '7', '7', '9', '6', 'NULL', 'NULL', '7', '8'] 
['2015-07-27 15:32:15.000', '345', '99', '111', '110', '139', '101', '86', 'NULL', 'NULL', '106', '120', '21', '37', '44', '64', '58', '28', 'NULL', 'NULL', '7', '6', '5', '8', '12', '8', 'NULL', 'NULL', '6', '10'] 
+0

您在'for'循环中隐藏'item'。为其中一个使用另一个名称。例如'items = []',然后是'items.extend()',因为您使用列表进行扩展,而不是单个项目。 – tayfun

+0

这不是一个很好的方法来做你想做的事情。我不认为你完全明白你想要做的是什么。你能否澄清一下,你是否想用0替换csv文件中每行的NULL? – siphr

回答

0

您正在使用row意味着两个不同的东西。将您的for循环更改为:

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