午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

您的位置:首頁技術文章
文章詳情頁

python各種excel寫入方式的速度對比

瀏覽:141日期:2022-07-05 18:35:11

經過實驗,新建一個excel表格,該表格擁有7個sheet,每個sheet有800條數據,其中最后一個sheet為空。

首先使用openpyxl進行寫入操作,代碼如下:

book = openpyxl.Workbook()auths = Auth.objects.filter(owner_id=1)filename = ’導出數據’for auth in auths: sheet = book.create_sheet(auth.name, index = 0) sheet.append([ _('書名'), _('作者'), _('譯者'), _('出版社'), _('序列號'), _('總頁數'), ]) objs = None objs = Book.objects.filter(owner_id=auth.id) for u in objs: data = [] data.append(u.name) data.append(auth.name) data.append(u.translator) data.append(u.press) data.append(u.serializer) data.append(u.page) sheet.append(data)return ExcelBookResponse(book, filename)

使用xlwt寫入數據:

book = xlwt.Workbook()auths = Auth.objects.filter(owner_id=1)filename = ’導出數據’for auth in auths: sheet = book.add_sheet(sensor.name) sheet.write(0, 0, _('書名')) sheet.write(0, 1, _('作者')) sheet.write(0, 2, _('譯者')) sheet.write(0, 3, _('出版社')) sheet.write(0, 4, _('序列號')) sheet.write(0, 5, _('總頁數')) i = 1 objs = None objs = Book.objects.filter(owner_id=auth.id) for u in objs: sheet.write(i, 0, u.name) sheet.write(i, 1, auth.name) sheet.write(i ,2,u.translator) sheet.write(i ,3,u.press) sheet.write(i, 4, u.serializer) sheet.write(i, 5, u.page) i += 1return ExcelBookResponse(book, filename)

使用XlsxWriter寫入數據:

book = xlsxwriter.Workbook(output)auths = Auth.objects.filter(owner_id=1)for auth in auths: sheet = book.add_worksheet(sensor.name) header = [ _('書名'), _('作者'), _('譯者'), _('出版社'), _('序列號'), _('總頁數'), ] sheet.write_row('A1', header) objs = Book.objects.filter(owner_id=auth.id) i = 1 for u in objs: sheet.write(i, 0, u.name) sheet.write(i, 1, auth.name) sheet.write(i ,2,u.translator) sheet.write(i ,3,u.press) sheet.write(i, 4, u.serializer) sheet.write(i, 5, u.page) i += 1book.close()file_ext = ’xlsx’mimetype = ’application/vnd.openxmlformats-officedocument.spreadsheetml.sheet’# self[’Content-Disposition’] = ’attachment; filename*=UTF-8’’'{2}.{1}'; filename='{0}.{1}'’.format(filename.replace(’'’, ’'’), file_ext, urllib.parse.quote(filename.replace(’'’, ’'’))).encode(’utf8’)return HttpResponse(content=output.getvalue(), content_type=mimetype)

三者的時間比較(兩種方式的文件內容是一樣的):

openpyxl: 文件大小為110.75kb, 平均時間大約為570ms

xlwt: 文件大小為505.91kb,平均時間大約為440ms

XlsxWrite: 文件大小為109.28kb,平均時間大約為500ms

xlwt寫入的行數有限制,因此對于較大的文件來說,XlsxWrite的速度較快一點

補充知識:python寫入excel文件太慢如何解決-python往excel寫入大量數據

目前用的openpyxl,從數據庫獲取8W行的數據通過openpyxl寫入excel,要花費接近8分鐘,這也太慢了,用kettle的插件秒進,python有什么方法能提升速度么,或者openpyxl能批量插入么,按行效率太低了

#!/usr/bin/python# -*- coding: UTF-8 -*-from openpyxl import Workbook as wbookdef xlsx(filename, rows_info, sheet=’Result’):if filename and sheet:wb = wbook()_sheet = wb.active_sheet.title = sheetrow = _sheet.max_rowfor line in rows_info:if isinstance(line, str):row_list = [line]elif isinstance(line, dict):row_list = list(line.values())else:try:row_list = list(line)except:row_list = []for col in range(0, len(row_list)):col_info = row_list[col]_sheet.cell(row, col + 1, col_info)row += 1wb.save(filename)else:return ’文件和sheet不能為空’

以上這篇python各種excel寫入方式的速度對比就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: python
相關文章:
主站蜘蛛池模板: 最新日韩中文字幕 | 欧美激情性做爰免费视频 | 超碰人人av | 精品久久免费 | 亚洲成人精品久久 | 黄色免费影片 | 成人免费区一区二区三区 | 夜夜嗨av一区二区三区网页 | 亚洲婷婷免费 | 日韩中文字幕网站 | www黄色网| 天天操免费视频 | 欧美成人午夜精品免费 | 免费视频久久 | 加勒比在线一区 | 另类毛片 | 91爱国产| 欧美在线中文 | 精品一区二区在线观看 | 乳色吐息免费看 | 太骚了全程淫语小说 | 日本欧美国产 | 国产成人精品久久二区二区91 | 麻豆视频一区 | 狠狠躁夜夜躁人人爽视频 | 亚色视频在线观看 | 久久九九精品 | 亚洲欧美国产精品专区久久 | 麻豆映画在线观看 | www狠狠干 | av在线一 | 高潮毛片无遮挡 | 亚洲精品福利 | 国产成人一级片 | 操中国女人的逼 | 欧美三级网站在线观看 | 日韩成人免费视频 | 精品手机在线视频 | 色导航在线| 午夜小视频在线观看 | 午夜性视频 |