[Tutor] Appending to CSV file puts data in wrong columns
Benjamin Fishbein
bfishbein79 at gmail.com
Thu Aug 29 16:00:26 EDT 2019
Here’s the code, using the csv module. It adds a new row to an existing file.
def save_sku_and_price_data_to_csv(sku, price, path):
row = {"sku": sku, "price": price}
with open(path, "a") as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=row.keys())
writer.writerow(row)
print("adding", row, "to", path)
Here’s the problem: It worked properly the first time I ran it, but today it has been writing the “sku”s in the “price” column and the “price”s in the “sku” column.
Any idea why this is or how to fix it?
More information about the Tutor
mailing list