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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:00:22+00:00 2026-06-04T00:00:22+00:00

So far all I am able to do within the function is store all

  • 0

So far all I am able to do within the function is store all the data twice over.

import csv
def csvWriter(filename, records): 
  header = []
  for i in records:
    for v in i:
      header.append(v)
  test = open(filename,'w')
  dict_wr = csv.DictWriter(test,header)
  dict_wr.writerow(dict(zip(header,header)))
  for i in records:
    dict_wr.writerow(dict(zip(header,i.values())))
  test.close()
  return '%d records processed.' % len(records)

File contains:

a,b,a,b
1,2,1,2
3,4,3,4

I believe I found the problem, inside the for loop, I’m having trouble creating the proper header.

  • 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-06-04T00:00:24+00:00Added an answer on June 4, 2026 at 12:00 am

    It looks like your records in this example are 2 dictionaries with the same keys (‘a’ and ‘b’). You can fix this by getting the header from the first dictionary:

    for i in records[0]:
        for v in i:             
            header.append(v)
    

    However, if your other records have unique key values, you’ll probably want to include them in the header:

    for i in records:
        for v in i:
            if v not in header: header.append(v)
    

    Finally, if you have repeated values in the header, and you have DictWriter write a row, it will repeat entries for each duplicated header value. For example,

    import csv
    testfile = open("test.csv", "w")
    header = ["a","b", "a","b"]                      #duplicate header fields
    writer = csv.DictWriter(testfile, header)
    writer.writerow(dict(zip(header, header)))
    writer.writerow({header[0]:"A", header[1]:"B"})  #notice it writes 4 values to the row
    testfile.close()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really have no idea how to go about this. So far all I
Not sure how to word the question... Basically, so far all my SQL stuff
I read a few websites and questions but all were far beyond my level
Thus far used sql server stored procedures for all my web applications... Now thought
I followed all of the instructions so far from: http://code.google.com/p/appengine-jruby/wiki/RunningRails and http://gist.github.com/268192 Currently, I'm
In all of the demos I have seen thus far, Web Workers and Web
So far during my site development I have been storing all my jQuery functions
All my apps that i've created so far do this. It does nothing to
So far I've been chugging along with Parcelable objects without issue, mainly because all
I want to get all conflict documents from a Notes database. So far, i've

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.