Help with practice problem: 1000351-7

Screen Link: https://app.dataquest.io/m/1000351/cleaning-and-preparing-data-in-python-practice-problems/7/cleaning-house-listings-3

My Code:


def write_csv(rows):
    f = open('listings_clean.csv', mode='w')
    writer = csv.writer(f)
    for row in rows:
        writer.writerow(row)
    f.close()

def clean_num_rooms_col():
    # Read the CSV file
    from csv import reader
    opened_file = open('listings.csv')  
    read_file = reader(opened_file)
    rows = list(read_file)
    rows = rows[0]
    # Clean num_rooms col
    id_in_list = []
    id_not_in_list = []
    range_no = (1000, 9999)
    for row in rows[1:]:  
        id_no = row[0]
        if id_no not in id_in_list:
            id_in_list.append(id_no)
    for i in range_no:
        if str(i) not in id_in_list:
            id_not_in_list.append(str(i))
    for row in rows[1:]:
        id_no = row[0]
        if len(id_no) != 4:
            id_no = id_not_in_list[0]
            id_not_in_list.remove(id_not_in_list[0])
    write_csv(rows)


What I expected to happen:

- List A: Create a list of ID's, current in listings.csv
- List B: Create a list of ID's, not in listings.csv
- Append blank ID values, with one from list B


What actually happened: 
Error I got: Wrong header on column number 1. Expected id but found 7332.


Any help on where I've gone wrong would be really appreciated!!
1 Like

rows is a list of lists. But you assigned back to it only the first list when you did:

rows = rows[0]

From now on rows only contain one list.

1 Like