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

  • Home
  • SEARCH
  • 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 493793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:26:19+00:00 2026-05-13T05:26:19+00:00

For example, my csv has columns as below: ID, ID2, Date, Job No, Code

  • 0

For example, my csv has columns as below:

ID, ID2, Date, Job No, Code

I need to write the columns back in the same order. The dict jumbles the order immediately, so I believe it’s more of a problem with the reader.

  • 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-13T05:26:19+00:00Added an answer on May 13, 2026 at 5:26 am

    Python’s dicts do NOT maintain order prior to 3.6 (but, regardless, in that version the csv.DictReader class was modified to return OrderedDicts).

    However, the instance of csv.DictReader that you’re using (after you’ve read the first row!-) does have a .fieldnames list of strings, which IS in order.

    So,

    for rowdict in myReader:
      print ['%s:%s' % (f, rowdict[f]) for f in myReader.fieldnames]
    

    will show you that the order is indeed maintained (in .fieldnames of course, NEVER in the dict — that’s intrinsically impossible in Python!-).

    So, suppose you want to read a.csv and write b.csv with the same column order. Using plain reader and writer is too easy, so you want to use the Dict varieties instead;-). Well, one way is…:

    import csv
    
    a = open('a.csv', 'r')
    b = open('b.csv', 'w')
    ra = csv.DictReader(a)
    wb = csv.DictWriter(b, None)
    
    for d in ra:
    
      if wb.fieldnames is None:
        # initialize and write b's headers
        dh = dict((h, h) for h in ra.fieldnames)
        wb.fieldnames = ra.fieldnames
        wb.writerow(dh)
    
      wb.writerow(d)
    
    b.close()
    a.close()
    

    assuming you have headers in a.csv (otherewise you can’t use a DictReader on it) and want just the same headers in b.csv.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 302k
  • Answers 302k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer By writing the code in this way, you've basically made… May 13, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer To achieve a table layout like that you just need… May 13, 2026 at 8:21 pm
  • Editorial Team
    Editorial Team added an answer You could use Google Analytics? They have an API that… May 13, 2026 at 8:21 pm

Related Questions

I'm trying to read data from a.csv file to ouput it on a webpage
In my C# application I am using the Microsoft Jet OLEDB data provider to
I have 12 .csv files produced by another program. The .csv files contain numeric
I am trying to take a rather large CSV file and insert it into

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.