av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

python各種excel寫(xiě)入方式的速度對(duì)比

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

經(jīng)過(guò)實(shí)驗(yàn),新建一個(gè)excel表格,該表格擁有7個(gè)sheet,每個(gè)sheet有800條數(shù)據(jù),其中最后一個(gè)sheet為空。

首先使用openpyxl進(jìn)行寫(xiě)入操作,代碼如下:

book = openpyxl.Workbook()auths = Auth.objects.filter(owner_id=1)filename = ’導(dǎo)出數(shù)據(jù)’for auth in auths: sheet = book.create_sheet(auth.name, index = 0) sheet.append([ _('書(shū)名'), _('作者'), _('譯者'), _('出版社'), _('序列號(hào)'), _('總頁(yè)數(shù)'), ]) 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寫(xiě)入數(shù)據(jù):

book = xlwt.Workbook()auths = Auth.objects.filter(owner_id=1)filename = ’導(dǎo)出數(shù)據(jù)’for auth in auths: sheet = book.add_sheet(sensor.name) sheet.write(0, 0, _('書(shū)名')) sheet.write(0, 1, _('作者')) sheet.write(0, 2, _('譯者')) sheet.write(0, 3, _('出版社')) sheet.write(0, 4, _('序列號(hào)')) sheet.write(0, 5, _('總頁(yè)數(shù)')) 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寫(xiě)入數(shù)據(jù):

book = xlsxwriter.Workbook(output)auths = Auth.objects.filter(owner_id=1)for auth in auths: sheet = book.add_worksheet(sensor.name) header = [ _('書(shū)名'), _('作者'), _('譯者'), _('出版社'), _('序列號(hào)'), _('總頁(yè)數(shù)'), ] 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)

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

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

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

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

xlwt寫(xiě)入的行數(shù)有限制,因此對(duì)于較大的文件來(lái)說(shuō),XlsxWrite的速度較快一點(diǎn)

補(bǔ)充知識(shí):python寫(xiě)入excel文件太慢如何解決-python往excel寫(xiě)入大量數(shù)據(jù)

目前用的openpyxl,從數(shù)據(jù)庫(kù)獲取8W行的數(shù)據(jù)通過(guò)openpyxl寫(xiě)入excel,要花費(fèi)接近8分鐘,這也太慢了,用kettle的插件秒進(jìn),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寫(xiě)入方式的速度對(duì)比就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 亚洲一区二区三区视频 | 免费的黄色小视频 | 亚洲天天干 | 日韩黄色在线视频 | 国产精品美女在线观看 | 日韩不卡在线观看 | 日本黄色免费看 | 成人永久免费视频 | 黑人操亚洲女人 | 欧洲精品一区 | 简单av网| 国产福利网站 | 91爱看| 久久国产影院 | 一级黄色片网站 | 日日干日日 | 日韩精品三区 | 亚洲精品视频免费 | 97人人插 | 婷婷五月在线视频 | 欧美日韩免费在线 | 九色91popny蝌蚪| 免费视频a | 精品1区| 婷久久 | 国产精品福利视频 | 天天操狠狠操 | 在线观看免费黄色 | 亚洲va视频 | 欧美色偷偷 | 免费特级毛片 | 国产成人小视频 | 国产视频一区在线 | 成人免费公开视频 | 国产日韩欧美一区 | 天天操天天干天天操 | 玖玖在线视频 | 国产麻豆91| 91成人免费版 | 免费性爱视频 | 992tv在线|