I am trying to delete particular columns in csv file.
CSV file:
Name,Age,YearofService,Department,Allocation
Birla,49,12,Welding,Production
Robin,38,10,Molding,Production
I am trying to delete Entire column having column header "Department" and "Allocation".
My code :
with open(input.csv,'r') as i:
with open(output.csv,'w',new line='') as o:
reader=csv.reader(i)
writer = csv.writer(o)
for row in reader:
for i in range(len(row)):
if row[i]!="Department" and row[i]!="Allocation":
writer.writerow(row)
My output:
Name
Birla
Robin
Age
49
38
YearofService
12
10
Expected output:
Name,Age,YearofService
Birla,49,12
Robin,38,10
We cannot gaurantee Department and Allocation will be in column header position "3" and "4". thats y am using iteration through length of row