Cleaning and preparing data in python practice problem

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

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()
# Write your code below
def clean_available_col():
    #Read the CSV file
    from csv import reader
    f = open('listings.csv')
    reader = reader(f)
    rows = list(reader)
    #clean last column
    for row in rows:
        if row[-1] == 'y' or row[-1] == 'Y':
            row[-1] = 'yes'
        if row[-1] == 'n' or row[-1] == 'NO':
            row[-1] = 'no'
    #write the cleaned file 
    write_csv(rows)
    
    
#call the funtion and check results
clean_available_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:

Replace this line with the output/error

I don’t understand what this line actually does ‘writer.writerow(row)’ in the above code

1 Like

Hi @harsharam911: You can find the explanation to this simply by googling it as seen in this article.

The writerows() method essentially does this:

def writerows(self, rows):
    for row in rows:
        self.writerow(row)

where each row must be a sequence of columns. A string is a sequence of individual characters, so that’s what you get written: individual characters separated by your chosen delimiter.

Hope this helps!

1 Like