I want to re-arrange headers in csv file where i need specific filednames coming consequetively and followed with rest. I have a count of 250 columns/headers in my csv file. Do any one know how to achieve it?
In this example the headers are only 5 [A,B,C,D,E]
But i have 250 headers in my csv and in need to write 250 filed names instead of 5 field names which is not ok:
import csv with open('file.csv', 'r') as infile, open('reordered.csv', 'a') as outfile: # output dict needs a list for new column ordering fieldnames = ['A', 'C', 'D', 'E', 'B'] writer = csv.DictWriter(outfile, fieldnames=fieldnames) # reorder the header first writer.writeheader() for row in csv.DictReader(infile): # writes the reordered rows to the new file writer.writerow(row)
Can you please suggest any other way , i can set up the sequence of the header when we woek on lots of data??