我想追加'str'对象

时间:2016-02-15 14:33:48

标签: python list append attributeerror

我在代码中将“NULL”项转换为0。然后,我必须将它们附加到列表中,我想为s1,s2..s8创建一个列表。

附加时我有这个属性错误:'str'对象没有属性'append'。如果有人可以帮助我,我会很高兴。

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']

1 个答案:

答案 0 :(得分: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))