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 834553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:41:22+00:00 2026-05-15T04:41:22+00:00

Assume I have a csv.DictReader object and I want to write it out as

  • 0

Assume I have a csv.DictReader object and I want to write it out as a CSV file. How can I do this?

I know that I can write the rows of data like this:

dr = csv.DictReader(open(f), delimiter='\t')
# process my dr object
# ...
# write out object
output = csv.DictWriter(open(f2, 'w'), delimiter='\t')
for item in dr:
    output.writerow(item)

But how can I include the fieldnames?

  • 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-15T04:41:23+00:00Added an answer on May 15, 2026 at 4:41 am

    Edit:
    In 2.7 / 3.2 there is a new writeheader() method. Also, John Machin’s answer provides a simpler method of writing the header row.
    Simple example of using the writeheader() method now available in 2.7 / 3.2:

    from collections import OrderedDict
    ordered_fieldnames = OrderedDict([('field1',None),('field2',None)])
    with open(outfile,'wb') as fou:
        dw = csv.DictWriter(fou, delimiter='\t', fieldnames=ordered_fieldnames)
        dw.writeheader()
        # continue on to write data
    

    Instantiating DictWriter requires a fieldnames argument.
    From the documentation:

    The fieldnames parameter identifies
    the order in which values in the
    dictionary passed to the writerow()
    method are written to the csvfile.

    Put another way: The Fieldnames argument is required because Python dicts are inherently unordered.
    Below is an example of how you’d write the header and data to a file.
    Note: with statement was added in 2.6. If using 2.5: from __future__ import with_statement

    with open(infile,'rb') as fin:
        dr = csv.DictReader(fin, delimiter='\t')
    
    # dr.fieldnames contains values from first row of `f`.
    with open(outfile,'wb') as fou:
        dw = csv.DictWriter(fou, delimiter='\t', fieldnames=dr.fieldnames)
        headers = {} 
        for n in dw.fieldnames:
            headers[n] = n
        dw.writerow(headers)
        for row in dr:
            dw.writerow(row)
    

    As @FM mentions in a comment, you can condense header-writing to a one-liner, e.g.:

    with open(outfile,'wb') as fou:
        dw = csv.DictWriter(fou, delimiter='\t', fieldnames=dr.fieldnames)
        dw.writerow(dict((fn,fn) for fn in dr.fieldnames))
        for row in dr:
            dw.writerow(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that is reading a remote CSV file, and adding
Similar to this question I have a small groovy test script that basically uses
Assume I have a class like this: public class Foo { public Bar RequiredProperty
Assume I have SomeExtension MarkupExtension. Does anyone know how to assign it to a
I have data stored in comma delimited txt files. One of the columns represents
Ok, running into a problem with pulling a Zip code from a CSV file
Assume I have the following string constants: const string constString1 = Const String 1;
Assume I have one matrix with two columns. Every column has thousands of values.
Assume I have a interface like: @interface it:NSObject { NSString* string; } @end @implement
I'm trying to produce a plot with two lines using data taken from stdin.

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.