Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:56:00+00:00 2026-05-22T16:56:00+00:00

I realize this is very similar to this question . However, I have a

  • 0

I realize this is very similar to this question. However, I have a CSV file that always comes in the same format that I need to write out with columns in a different order to move it down the data processing pipeline. If my csv file contains headers and data like this:

Date,Individual,Plate,Sample,test,QC
03312011,Indiv098,P342,A1,deep,passed
03312011,Indiv113,P352,C3,deep,passed

How would I write out a csv file with the same columns as the original input csv but in the following order:

test,QC,Plate,Sample
deep,passed,P342,A1
deep,passed,P352,C3

My initial thought was to do something like this:

f = open('test.csv')
lines = f.readlines()
for l in lines:
    h = l.split(",")
    a, b, c, d, e, f  = h
    for line in h:
        print e, f, c, d, 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:56:01+00:00Added an answer on May 22, 2026 at 4:56 pm

    If there’s the slightest chance that the input file or the output file won’t have the same layout each time, here’s a more general way to get your “reorderfunc”:

    writenames = "test,QC,Plate,Sample".split(",") # example
    reader = csv.reader(input_file_handle)
    writer = csv.writer(output_file_handle)
    # don't forget to open both files in binary mode (2.x)
    # or with `newline=''` (3.x)
    readnames = reader.next()
    name2index = dict((name, index) for index, name in enumerate(readnames))
    writeindices = [name2index[name] for name in writenames]
    reorderfunc = operator.itemgetter(*writeindices)
    writer.writerow(writenames)
    for row in reader:
        writer.writerow(reorderfunc(row))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I realize that this is similar to this question , however, I only get
Firstly, I realise that this is a very similar question to this one: Which
I realize this is very similar to this question: how to set windows service
I realize this is more of a hardware question, but this is also very
This is a very elementary I realize, I have recently started working with asp.net
I realize this is a basic question but I have searched online, been to
I realize this question is pretty basic, but I'm really stuck. I have a
I realize there are a lot of similar questions as this one , however
I can't seem to find the answer to this question anywhere. I realize that
I realise this is a very similar question to Stop SSMS from scripting SPs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.