Home > Net >  Re-ordering columns in a csv but with Dictionaries
Re-ordering columns in a csv but with Dictionaries

Time:03-31

I need to re-order columns in a csv but I'll need to call each column from a dictionary.

EXAMPLE:

Sample input csv File:

$ cat file.csv
A,B,C,D,E
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2

Code

import csv

with open('file.csv', 'r') as infile, open('reordered.csv', 'a') as outfile:
    order_of_headers_should_be = ['A', 'C', 'D', 'E', 'B']
    dictionary = {'A':'X1','B':'Y1','C':'U1','D':'T1','E':'K1'}
    writer = csv.DictWriter(outfile)
    # reorder the header first
    writer.writeheader()
    for row in csv.DictReader(infile):
        # writes the reordered rows to the new file
        writer.writerow(row)

The Output csv file needs to look like this:

$ cat reordered.csv
X1,U1,T1,K1,Y1
a1,c1,d1,e1,b1
a2,c2,d2,e2,b2

Trying to make a variable to call the dictionary

CodePudding user response:

You can do this by permuting the keys when you are about to write the row like so:

    for row in csv.DictReader(infile):
        # writes the reordered rows to the new file
        writer.writerow({dictionary[i]: row[i] for i in row})

Note the use of a dictionary comprehension.

CodePudding user response:

You're pretty close. All you need to do is give the new DictWriter a list of its new header names in the correct order, and provide a dictionary with those names.

order_of_headers_should_be = ['A', 'C', 'D', 'E', 'B']
dictionary = {'A':'X1','B':'Y1','C':'U1','D':'T1','E':'K1'}
new_headers = [dictionary[old_header] for old_header in order_of_headers_should_be]

with open('file.csv', 'r') as infile, open('reordered.csv', 'a') as outfile:
    writer = csv.DictWriter(outfile, fieldnames=new_headers)
    writer.writeheader()
    for row in csv.DictReader(infile):
        new_row = {dictionary[old_header]: row[old_header] for old_header in row}
        writer.writerow(new_row)

Which gives:

X1,U1,T1,K1,Y1
a1,c1,d1,e1,b1
a2,c2,d2,e2,b2

Explanation:

new_headers = [dictionary[old_header] for old_header in order_of_headers_should_be]

We iterate over order_of_headers_should_be, and for each element in this list (which is the value of the old column name old_header), we look up the correct value of the new column name in dictionary.

Similarly, to make the new_row:

new_row = {dictionary[old_header]: value for old_header, value in row.items()}

We iterate over every key-value pair in row. Keys are the old column names, so we look up the new column name from the dictionary. Then, we use a dict comprehension to make a new row

  • Related