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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:57:34+00:00 2026-05-19T06:57:34+00:00

I am processing some files and want to create a log of what I

  • 0

I am processing some files and want to create a log of what I am processing. I created the log by using a dictionary to hold the keys and values for each observation and then I am appending the dictionary to a list (a list of dictionaries).

To save the log I am using Python’s csv module to write out the list of dictionaries. Initially I was using writerows but I encountered a problem in that very infrequently some of the values I am storing are something other than ascii

example

 Investee\xe2\x80\x99s Share of Profits

my solution was to iterate through my list of dictionaries using try / except statements to skip over the problem dictionaries

for docnumb, item in enumerate(x[1]):
    try:
        dict_writer.writerow(item)
    except UnicodeEncodeError:
        missed.append(docnumb)
        item

However, this leads to an extra row being inserted in each line of the output csv file.

value1 value2 value3 etc . . .
#blank row
value1 value2 value3 etc

I can’t see how to suppress this behavior.

a little more code so there is more clarity about how I got here

import csv
keyset=set([])
for item in x[1]:
    keyset |=set(item.keys())
keys=list(keyset)
logref=open(r'c:\December_2010_File_list.csv','w')
dict_writer=csv.DictWriter(logref,keys)

keyset |=set(item.keys())
  • 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-19T06:57:36+00:00Added an answer on May 19, 2026 at 6:57 am

    See the documentation at http://docs.python.org/library/csv.html#csv-examples

    They give a UnicodeWriter class as follows:

    class UnicodeWriter:
        """
        A CSV writer which will write rows to CSV file "f",
        which is encoded in the given encoding.
        """
    
        def __init__(self, f, dialect=csv.excel, encoding="utf-8", **kwds):
            # Redirect output to a queue
            self.queue = cStringIO.StringIO()
            self.writer = csv.writer(self.queue, dialect=dialect, **kwds)
            self.stream = f
            self.encoder = codecs.getincrementalencoder(encoding)()
    
        def writerow(self, row):
            self.writer.writerow([s.encode("utf-8") for s in row])
            # Fetch UTF-8 output from the queue ...
            data = self.queue.getvalue()
            data = data.decode("utf-8")
            # ... and reencode it into the target encoding
            data = self.encoder.encode(data)
            # write to the target stream
            self.stream.write(data)
            # empty queue
            self.queue.truncate(0)
    
        def writerows(self, rows):
            for row in rows:
                self.writerow(row)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm using Tracesource to log some errors and wan't to create a log
I'm trying to create a framework to do some processing of files and data.
I'm doing some processing on some very large video files (often up to 16MP),
For example, I need to do some pre-processing of source files before compiling. How
I have some command-line ruby scripts for things like pre-processing text files before uploading
I'm using GAE[JAVA] to do some image processing. GAE will not allow write file
I have a large set of files for which some heavy processing needs to
I want to send an xml document that i have created using jdom api.
I am using background worker to do a long process on some files. I
I know some PHP and now I want to combine it using ajax/jquery. Just

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.