import csv def write_csv(rows): f = open('listings_clean.csv', mode='w') writer = csv.writer(f) for row in rows: writer.writerow(row) f.close() import random def clean_id_col(): f = open('listings.csv') read = csv.reader(f) listings = list(read) listings_wo_header = listings[1:] id_set = set() # Create a set because set does not allow duplicates for row in listings_wo_header: random_num = random.sample(range(1000,9999), 1) # If 'id' is blank, we set it as a random number, then we will add it to the set. If it is not blank, we will just add the 'id' number to the set. if row == '': row = str(random_num) id_set.add(str(row)) if row in id_set: # This step to make sure there is no duplicates row = str(random_num) # if there is, we will generate another random number elif row != '': id_set.add(str(row)) write_csv(listings) clean_id_col() f = open('listings_clean.csv') reader = csv.reader(f) rows = list(reader) for i in range(30): print(rows[i])
I use random number generator, although there is no prior lessons about it. I think it works better for this problem since I did not fully understand the answer given by the assignment, I come up with my own.
The answer from the code above turns out correct. But I would love to know people’s thoughts on this. Do you understand this better? What other code would you replace/add to it?