我想附加 'str' 对象

I want to append 'str' object

我在代码中将 "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']

您使用 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))