Python Forum

Full Version: Rearrange Columns in a CSV File
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I've got a file that I am trying to reformat into a new file, but am having issues with the code I am using. It is adding a blank row in between every line in the file.

Here is what I need to do:
1) Open any File with a "*-100.csv" and Write it to "*-102.csv". Ideally the star would stay consistent (i.e. file 1-100.csv would create 1-102.csv and file "2-100.csv" would create "2-102.csv"

Edit the first line of the file to give it headers (not sure if this is necessary, but since there aren't column headers for every column, I don't know a way around this). In my current code, I've used the following headers:
A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,AA,AB,AC,AD,AE,AF,AG,AH,AI,AJ,AK

2) Rearrange the Columns in the following order: ['A','B','C','H', 'I', 'D', 'E', 'F', 'G','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK']

3) Delete the column header


Here is what I have that is giving me issues with the added blank lines in between each row of data. I'm struggling to figure out how to add the column headers to the first line of the file and then removing them after, so that's not currently in the code at all.

import csv

with open("C:\\csv\\041-100.csv", 'r') as infile, open("C:\\csv\\041-102.csv", 'a') as outfile:
    # output dict needs a list for new column ordering
    fieldnames = ['A','B','C','H', 'I', 'D', 'E', 'F', 'G','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK']
    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)
Thanks in advance for your help!
Hi,
The first question before 1-2-3 is about the extra line.
I suspect that you are reading some CRLF from the old file.
In python they call that newline.
You may try to read the row and then do:
row = row[:-1] that takes away the old escape sequence \n

1) Would be simpler to answer if the directory contains only files
that need to be transformed, or other stuff that looks like that.

Paul