Python Forum
CSV file column swapping
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV file column swapping
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(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)
Reply
#4
Oh, try replacing writerow with writerows on line 10.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(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)
Reply
#6
Oh nevermind. It's functional. Thanks again!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  To extract a specific column from csv file and compute the average vicson 2 8,066 Oct-20-2018, 03:18 AM
Last Post: vicson

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020