Search code examples
pythonlistappendattributeerror

I want to append 'str' object


I converted "NULL" item to 0 in my code. Then, I have to append them to a list and I want to create a list for s1, s2..s8.

I have this attribute error while appending: 'str' object has no attribute 'append'. If somebody could help me, I will be happy.


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

input:

rows:

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

Solution

  • You are using row to mean two different things. Change your for loop to this:

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