Wednesday, April 11, 2012

Python - SQLite to CSV Writer Error - ASCII values not parsed


I am having some trouble with a SQLite to CSV python script. I have searched high and I have searched low for an answer but none have worked for me, or I am having a problem with my syntax.

I want to replace characters within the SQLite database which fall outside of the ASCII table (larger than 128).

Here is the script I have been using:

import sqlite3
import csv, codecs, cStringIO

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) = f
self.encoder = codecs.getincrementalencoder(encoding)()

def writerow(self, row):
self.writer.writerow([unicode(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
# empty queue

def writerows(self, rows):
for row in rows:

conn = sqlite3.connect('test.db')

c = conn.cursor()

# Select whichever rows you want in whatever order you like
c.execute('select ROWID, Name, Type, PID from PID')

writer = UnicodeWriter(open("ProductListing.csv", "wb"))

# Make sure the list of column headers you pass in are in the same order as your SELECT
writer.writerow(["ROWID", "Product Name", "Product Type", "PID", ])

I have tried to add the 'replace' as indicated here but have got the same error. Python: Convert Unicode to ASCII without errors for CSV file

The error is the UnicodeDecodeError.

Traceback (most recent call last):
File "", line 53, in <module>
File "", line 32, in writerows
File "", line 19, in writerow
self.writer.writerow([unicode(s).encode("utf-8") for s in row])
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 65: ordinal not in range(128)

Obviously I want the code to be robust enough that if it encounters characters outside of these bounds that it replaces it with a character such as '?' (\x3f).

Is there a way to do this within the UnicodeWriter class? And a way I can make the code robust that it won't produce these errors.

Your help is greatly appreciated.

No comments:

Post a Comment