在 python 中导出 utf-8 csv 文件时遇到错误。错误说
AttributeError: 'int' object has no attribute 'encode'
首先,我使用 pyodbc 连接 microsoft access 数据库并在那里获取数据。
MDB = "E:/Research/2000-01.mdb"; DRV = '{Microsoft Access Driver (*.mdb)}'; PWD = 'pw'
con = pyodbc.connect('DRIVER={};DBQ={};PWD={}'.format(DRV,MDB,PWD))
cur = con.cursor()
SQL = 'SELECT * FROM 200001;'
rows = cur.execute(SQL).fetchall()
cur.close()
con.close()
然后使用类,
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)
我开始写 utf-8 csv 文件
with open("E:/Research/200001.txt", 'wb') as f:
writer = UnicodeWriter(f)
writer.writerows(rows)
行中的示例行是
(577540, u'1', datetime.datetime(2000, 1, 1, 0, 0), u'85411000', u'53', u'4403944851', u'44039', u'10', u'116', u'110', u'4', u'01', 89956, 0.15575717389583588, u'\u5916\u5546\u72ec\u8d44\u4f01\u4e1a', u'\u5c71\u7279\u7535\u5b50 (\u6df1\u5733) \u6709\u9650\u516c\u53f8', u'\u6df1\u5733\u5b9d\u5b8972\u533a\u5b9d\u77f3\u8def\u53f7', u'755 27757943', u'', u'518101', u'', u'\u90d1\u66fc\u5a1c', u'\u4e8c\u6781\u7ba1\uff0c\u4f46\u5149\u654f\u4e8c\u6781\u7ba1\u6216\u53d1\u5149\u4e8c\u6781\u7ba1\u9664\u5916', u'\u5e7f\u4e1c\u7701\u6df1\u5733', u'\u65e5\u672c', u'\u6df1\u5733\u6d77\u5173', u'\u4e00\u822c\u8d38\u6613', u'\u6c7d\u8f66\u8fd0\u8f93', u'\u4e2a/\u5957', u'\u9999\u6e2f', u'\u8fdb\u53e3')
看起来每一行都包含一些整数和日期时间的东西。有解决这个问题的想法吗?非常感谢!