Home > Enterprise >  Python csv dictionary calling from a dictionary to repeat rows in a csv according to dictionary
Python csv dictionary calling from a dictionary to repeat rows in a csv according to dictionary

Time:05-06

Hi everyone so I have a question in regards to a issue I am having.

Here is a sample input csv:

Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A,B1,C1,D1,E1,F1,G1,H1,I1,J1,
A,B2,C2,D2,E2,F2,G2,H2,I2,J2,1
B,B3,C3,D3,E3,F3,G3,H3,I3,J3,
B,B4,C4,D4,E4,F4,G4,H4,I4,J4,

This is a version of a code that i have:

import csv

fieldnames_dict = {
    'Alfa': 'Alfa_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()
        for row_in in reader:
            row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
            writer.writerow(row_out)

What this code does, is pretty much rearrange the the columns according to the dictionary.

However I need to repeat some rows of the csv file according to a new dictionary and also rename the value of that column's row.

For example:

second_dictionary = {
    'A' : '1ST,2ND,3RD",
    'B' : '4TH',
    }

This dictionary will need to be compared with the column "Alfa") and when it finds that it has the values of 'A', it will then repeat that row with the values of '1ST', '2ND' and '3RD'. If the column "Alfa" has the value of 'B', it will look at the dictionary and replace only once by it self as the 4TH.

What the output should look like:

Alfa_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
1ST,E1,F1,H1,I1,C1
2ND,E1,F1,H1,I1,C1
3RD,E1,F1,H1,I1,C1
1ST,E2,F2,H2,I2,C2
2ND,E2,F2,H2,I2,C2
3RD,E2,F2,H2,I2,C2
4TH,E3,F3,H3,I3,C3
4TH,E4,F4,H4,I4,C4

As you can see, since the input file had this row:

Alfa,Beta,Charlie,Delta,Echo,Foxtrot,Golf,Hotel,India,Juliett,Kilo
A,B1,C1,D1,E1,F1,G1,H1,I1,J1,

With the second dictionary, it will need to replace the 'A' from Alfa and copy it self 3 times but replacing the values from the second dictionary:

Alfa_New,Echo_New,Foxtrot_New_ALL,Hotel_New,India_New,Charlie_New
1ST,E1,F1,H1,I1,C1
2ND,E1,F1,H1,I1,C1
3RD,E1,F1,H1,I1,C1

What will I need to do for that?

CodePudding user response:

Maybe this could help:

import csv

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

second_dictionary = {
    'A': '1ST,2ND,3RD',
    'B': '4TH',
}

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()
        for row_in in reader:
            row_out = {new: row_in[old] for old, new in fieldnames_dict.items()}
            column_name = 'Alfa_New'
            column_key = row_out[column_name]
            if column_key in second_dictionary.keys():
                for item in second_dictionary[column_key].split(','):
                    row_out[column_name] = item
                    writer.writerow(row_out)
            else:
                writer.writerow(row_out)
  • Related