将mysql数据导出到excel会引发意外的datetime.datetime

lokaqttq  于 2021-06-21  发布在  Mysql
关注(0)|答案(0)|浏览(267)

我有这样的mysql数据
身份证|姓名|时间戳||
哪个时间戳字段包含日期时间格式的数据
下面是将mysql数据导出到xls的代码:

w = Workbook()
ws = w.add_sheet("Sheet Title") # worksheet name

# Database connection

con = MySQLdb.connect(host='localhost', user='', passwd='', db='dbtest')
cur = con.cursor()

cur.execute("SELECT name, time_stamp FROM log")

# iterate row and col

rowNum = 0
colNum = 0

# Print all to worksheet

for row in cur.fetchall():
   ws.write(rowNum, colNum, row)
   rowNum =+1
   colNum =+1

结果如下:

Traceback (most recent call last)
File "/usr/share/pyshared/flask/app.py", line 1836, in __call__
 return self.wsgi_app(environ, start_response)
File "/usr/share/pyshared/flask/app.py", line 1820, in wsgi_app
 response = self.make_response(self.handle_exception(e))
File "/usr/share/pyshared/flask/app.py", line 1403, in handle_exception
 reraise(exc_type, exc_value, tb)
File "/usr/share/pyshared/flask/app.py", line 1817, in wsgi_app
 response = self.full_dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1477, in full_dispatch_request
 rv = self.handle_user_exception(e)
File "/usr/share/pyshared/flask/app.py", line 1381, in handle_user_exception
 reraise(exc_type, exc_value, tb)
File "/usr/share/pyshared/flask/app.py", line 1475, in full_dispatch_request
 rv = self.dispatch_request()
File "/usr/share/pyshared/flask/app.py", line 1461, in dispatch_request
 return self.view_functions[rule.endpoint](**req.view_args)
File "/home/pi/script/app2.py", line 132, in report
 ws.write(rowNum, colNum, row)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Worksheet.py", line 1088, in write
 self.row(r).write(c, label, style)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 252, in write
 self.__rich_text_helper(col, label, style, style_index)
File "/usr/local/lib/python2.7/dist-packages/xlwt/Row.py", line 278, in __rich_text_helper
 raise Exception ("Unexpected data type %r" % type(data))
Exception: Unexpected data type <type 'datetime.datetime'>

我注意到,在尝试导出到xls时,sql数据中的datetime格式数据有错误。有没有办法将日期时间数据从sql导出到xls?已经在尝试stackoverflow的几种解决方案,但没有结果

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题