Python 忽略csv writer和MySQLdb的编码错误
我一直在尝试使用python将MySQL表中的完整数据转换为csv,我做得很好,但现在该表有一列“description”,现在简直是地狱,因为到处都有编码问题 在尝试了从其他帖子读到的大量内容后,我现在放弃了这些字符,我想直接跳过它们,避免这些错误 test.py:Python 忽略csv writer和MySQLdb的编码错误,python,csv,mysql-python,Python,Csv,Mysql Python,我一直在尝试使用python将MySQL表中的完整数据转换为csv,我做得很好,但现在该表有一列“description”,现在简直是地狱,因为到处都有编码问题 在尝试了从其他帖子读到的大量内容后,我现在放弃了这些字符,我想直接跳过它们,避免这些错误 test.py: import MySQLdb, csv, codecs dbConn = MySQLdb.connect(dbServer,dbUser,dbPass,dbName,charset='utf8') cur = dbConn.cur
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)
仍然从函数中获取该错误:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xd1' in position 27: ordinal not in range(128)
你知道我是否可以跳过这个错误,仍然从DB查询中写入其余的数据吗
PD:功能碰撞线为:
csv_writer.writerows(cur)
不知道这些信息对最终解决的人是否有用
更改:
import csv
csv_writer = csv.writer(csv_file)
致:
更改:
import csv
csv_writer = csv.writer(csv_file)
致: