Python Forum

Full Version: CSV file column swapping
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am trying to swap the values in columns 1 and 2 respectively from an input file (.csv) and have them displayed swapped in the output file (the other columns are still the same). Any help would be appreciated. For this assignment, I'm not allowed to use Pandas.

This is my current code:
def re_order(in_file, out_file):
    with open(in_file, 'r', newline='') as in_file_handle:
        reader = csv.reader(in_file_handle)
        columns = [1, 0]
        for row in reader:
            content = [row[i] for i in columns]
        with open(out_file, 'w', newline='') as out_file_handle:
            writer = csv.writer(out_file_handle)
            writer.writerow(content)
You need to initialize content as an open list before the for loop, and them append the list you are creating on line 6 to content. As it is, content gets completely replace each time through the loop, so it ends up just being the last row.
(Mar-04-2019, 02:46 AM)ichabod801 Wrote: [ -> ]You need to initialize content as an open list before the for loop, and them append the list you are creating on line 6 to content. As it is, content gets completely replace each time through the loop, so it ends up just being the last row.

So something like this? The output file is still wrong though.

def re_order(in_file, out_file):
    with open(in_file, 'r', newline='') as in_file_handle:
        reader = csv.reader(in_file_handle)
        columns = [1, 0]
        content = []
        for row in reader:
            content.append([row[i] for i in columns])
        with open(out_file, 'w', newline='') as out_file_handle:
            writer = csv.writer(out_file_handle)
            writer.writerow(content)
Oh, try replacing writerow with writerows on line 10.
(Mar-04-2019, 03:01 AM)ichabod801 Wrote: [ -> ]Oh, try replacing writerow with writerows on line 10.

That's exactly what I was missing...thanks so much!

Also changed what I appended.

def re_order(in_file, out_file):
    with open(in_file, 'r', newline='') as in_file_handle:
        reader = csv.reader(in_file_handle)
        content = []
        for row in reader:
            content.append([row[1]]+[row[0]]+row[2:])
        with open(out_file, 'w', newline='') as out_file_handle:
            writer = csv.writer(out_file_handle)
            writer.writerows(content)

(Mar-04-2019, 03:01 AM)ichabod801 Wrote: [ -> ]Oh, try replacing writerow with writerows on line 10.

Actually, I just realized now my code doesn't work for files that ONLY have 2 columns. How do I get it to work for files of 2 columns AND more?

def re_order(in_file, out_file):
    with open(in_file, 'r', newline='') as in_file_handle:
        reader = csv.reader(in_file_handle)
        content = []
        for row in reader:
            content.append([row[1]]+[row[0]]+row[2:])
        with open(out_file, 'w', newline='') as out_file_handle:
            writer = csv.writer(out_file_handle)
            writer.writerows(content)
Oh nevermind. It's functional. Thanks again!