ECA
ECA

Reputation: 309

Ignore encoding errors with csv writer and MySQLdb

I've been trying to get a full data from a MySQL table to csv with python and I've done it well but now that table has a column "description" and it's a piece of hell now coz there're encoding issues everywhere.

After trying tons and tons of things readed from other posts now i give up with those characters and i want to skip them directly and avoid those errors.

test.py:

import MySQLdb, csv, codecs
dbConn = MySQLdb.connect(dbServer,dbUser,dbPass,dbName,charset='utf8')
cur = dbConn.cursor()
def createFile(self):
    SQLview = 'SELECT fruit_type, fruit_qty, fruit_price, fruit_description FROM fruits'
    cur.execute(SQLview)
    with codecs.open('fruits.csv','wb',encoding='utf8',errors='ignore') as csv_file:
    csv_writer = csv.writer(csv_file)
    csv_writer.writerows(cur)

Still getting that error from the function:

UnicodeEncodeError: 'ascii' codec can't encode character u'\xd1' in position 27: ordinal not in range(128)

Any idea if i can skip that error and still writting the rest of the data from the DB Query?

PD: The function crash line is:

csv_writer.writerows(cur)

Don't know if that's usefull info for someone

Upvotes: 0

Views: 1180

Answers (1)

ECA
ECA

Reputation: 309

Finally solved

changed:

import csv

to:

import unicodecsv as csv

changed:

csv_writer = csv.writer(csv_file)

to:

csv_writer = csv.writer(csv_file,encoding='utf-8')

Upvotes: 1

Related Questions