I need the code to rearrange the columns while changing the header names without using Pandas. I also need to omit some of the columns in the new csv file.
Sample Input csv file:
Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A1,B1,C1,D1,E1,F1,G1,H1,I1,J1,K1
A2,B2,C2,D2,E2,F2,G2,H2,I2,J2,K2
A3,B3,C3,D3,E3,F3,G3,H3,I3,J3,K3
A4,B4,C4,D4,E4,F4,G4,H4,I4,J4,K4
A5,B5,C5,D5,E5,F5,G5,H5,I5,J5,K5
A6,B6,C6,D6,E6,F6,G6,H6,I6,J6,K6
A7,B7,C7,D7,E7,F7,G7,H7,I7,J7,K7
A8,B8,C8,D8,E8,F8,G8,H8,I8,J8,K8
A9,B9,C9,D9,E9,F9,G9,H9,I9,J9,K9
What I have so far:
import csv
#Opens and readers csv file
open_cd_csv = open("book1.csv", "r", encoding="utf-8", errors='ignore')
reader_cd_csv = csv.DictReader(open_cd_csv, delimiter=',', quotechar='"')
header_csv = next(reader_cd_csv)
#dictionary sample
dictionary_sample = {
"Beta_New" : reader_cd_csv["Beta"],
"Echo_New" : reader_cd_csv["Echo"],
"Foxtrot_New_All" : reader_cd_csv["Foxtrot"],
"Hotel_New" : reader_cd_csv["Hotel"],
"India_New" : reader_cd_csv["India"],
"Charlie_New" : reader_cd_csv["Charlie"],
}
#Opens and writes csv file
output_test_csv_file = "xtest_file.csv"
open_output_test_csv = open(output_test_csv_file, "w", encoding="utf-8", errors='ignore')
writer_output_test_csv = csv.writer(open_output_test_csv, delimiter=',', quotechar='"')
sample_list = []
for row in reader_cd_csv:
sample_list.append(dictionary_sample(row))
writer_output_test_csv.writerow(sample_list)
What the Output csv should look like:
Beta_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
B1,E1,F1,H1,I1,C1
B2,E2,F2,H2,I2,C2
B3,E3,F3,H3,I3,C3
B4,E4,F4,H4,I4,C4
B5,E5,F5,H5,I5,C5
B6,E6,F6,H6,I6,C6
B7,E7,F7,H7,I7,C7
B8,E8,F8,H8,I8,C8
B9,E9,F9,H9,I9,C9