I cannot really understand why the solution isn't accepted

Screen Link:

My Code:

import csv
from csv import reader

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

# Write your code below
def clean_num_rooms_col():
    f = open('listings.csv')
    r = reader(f)
    r_as_list = list(r)
    #print(r_as_list[:8])
    digits = '0123456789'
    rows = [r_as_list[0]]
    
    for row in r_as_list[1:]:
        rn = row[-2]
        for ch in rn:
            if ch not in digits:
                rn = rn.replace(ch, '')
        rn = int(rn)
        row[-2] = rn
        rows.append(row)
    write_csv(rows)
    
    
    
clean_num_rooms_col()
f = open('listings_clean.csv')
reader = csv.reader(f)
rows = list(reader)
for i in range(8):
    print(rows[i])

What I expected to happen:

What actually happened:

It is reported that "File listings_clean.csv does not have the expected content."


Click here to open the screen in a new tab.

Yeah, this one can be confusing. But they haven’t asked us to not modify the header row as well.

So, you need to clean all rows including the header row.

So, right now, when you print the first 8 rows, the first of those is -

[‘id’, ‘address’, ‘num_rooms’, ‘available’]

What it should be -

[‘id’, ‘address’, ‘’, ‘available’]

Your logic, otherwise, seems correct. You will just have to modify it a little bit to get the required answer.

Got that!
Thanks a lot! :beer:

1 Like