Home > Enterprise >  CSV adding values to an existing column and calling from a dictionary (no pandas)
CSV adding values to an existing column and calling from a dictionary (no pandas)

Time:04-30

I need to add some additional values to an existing column for my CSV file.

So this is what I have so far:

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,E1,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

This is what I have so far, I am thinking of converting row_out into a list and then inputing the values under India_New.

import csv

fieldnames_dict = {
    'Beta': 'Beta_New',
    'Echo': 'Echo_New',
    'Foxtrot': 'Foxtrot_New_ALL',
    'Hotel': 'Hotel_New',
    'India': 'India_New',
    'Charlie': 'Charlie_New'
}

with open("book1.csv", "r", encoding="utf-8", errors='ignore') as csv_in:
    with open("xtest_file.csv", "w", encoding="utf-8", errors='ignore') as csv_out:
        reader = csv.DictReader(csv_in, delimiter=',', quotechar='"')
        writer = csv.DictWriter(csv_out, delimiter=',', quotechar='"',
                                fieldnames=list(fieldnames_dict.values()))
        writer.writeheader()
            additional_values = [';I_1;I_2']
            new_row_out = []
            for row_in in reader:
            row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
            row_out.items()
                for row in row_out.items():
                new_row_out.append(row_out.items())
                writer.writerow(new_row_out)

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;I_1;I_2,C1
B2,E2,F2,H2,I2;I_1;I_2,C2
B3,E3,F3,H3,I3;I_1;I_2,C3
B4,E4,F4,H4,I4;I_1;I_2,C4
B5,E5,F5,H5,I5;I_1;I_2,C5
B6,E6,F6,H6,I6;I_1;I_2,C6
B7,E7,F7,H7,I7;I_1;I_2,C7
B8,E8,F8,H8,I8;I_1;I_2,C8
B9,E9,F9,H9,I9;I_1;I_2,C9

CodePudding user response:

Since writer is a DictWriter, the argument to writer.writerow() must be a dictionary, not a list like new_row_out.

Just append the string to the India_New item in the row_out dictionary before writing it.

with open("book1.csv", "r", encoding="utf-8", errors='ignore') as csv_in, open("xtest_file.csv", "w", encoding="utf-8", errors='ignore') as csv_out:
    reader = csv.DictReader(csv_in, delimiter=',', quotechar='"')
    writer = csv.DictWriter(csv_out, delimiter=',', quotechar='"',
                            fieldnames=list(fieldnames_dict.values()))
    writer.writeheader()
    additional_india_new = ';I_1;I_2'
    for row_in in reader:
        row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
        row_out['India_New']  = additional_india_new
        writer.writerow(row_out)
  • Related