I'm pretty new to python and I'm attempting to take a parts list from my furniture software and organize it in a way that works better. I'm starting with a csv file that writes each piece as its own item. So if I have 2 chair legs that are the same piece, I get
Leg 1,x,y,z,notes
Leg 2,x,y,z,notes
What I would like to end up with is
2,Leg 1,x,y,z,notes
where the first column is now quantity and is updated whenever python sees a new row with the same x,y,z,notes.
Right now I have a code, which I found most of from another post, that can write a file that eliminates duplicates, and it seems like I should be able to make it add quantities relatively easily, but I can't quite figure it out. I've looked at other posts, but haven't seen anything about updating within the same file, only comparing one file to another.
Here is the code I've been using to eliminate duplicates:
import csv
input_file = 'Brook Field 4 Drawer.csv'
output_file = 'updated_quantities.csv'
with open(input_file, 'rb') as infile, open(output_file, 'wb') as outfile:
incsv = csv.reader(infile)
outcsv = csv.writer(outfile)
pieces = set()
for row in incsv:
piece = tuple(row[1:4])
if piece not in pieces:
outcsv.writerow(row)
pieces.add(piece)
Can anyone suggest a solution? Am I even on the right track at the moment?