亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2020年11月10日 09:29:09   作者:左手小兜  
這篇文章主要介紹了python各種excel寫入方式的速度對比,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

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

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

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ù)"),
    ])
  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寫入數(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, _("書名"))
  sheet.write(0, 1, _("作者"))
  sheet.write(0, 2, _("譯者"))
  sheet.write(0, 3, _("出版社"))
  sheet.write(0, 4, _("序列號"))
  sheet.write(0, 5, _("總頁數(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 += 1
return ExcelBookResponse(book, filename)

使用XlsxWriter寫入數(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ù)"),
    ]
  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 += 1
book.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)

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

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

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

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

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

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

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

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from openpyxl import Workbook as wbook
def xlsx(filename, rows_info, sheet='Result'):
if filename and sheet:
wb = wbook()
_sheet = wb.active
_sheet.title = sheet
row = _sheet.max_row
for 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 += 1
wb.save(filename)
else:
return '文件和sheet不能為空'

以上這篇python各種excel寫入方式的速度對比就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python 代碼實現(xiàn)列表的最小公倍數(shù)

    Python 代碼實現(xiàn)列表的最小公倍數(shù)

    這篇文章主要介紹了Python 代碼實現(xiàn)列表的最小公倍數(shù),代碼實現(xiàn)了計算列表中元素的最小公倍數(shù)的功能,包括公式介紹,需要的朋友可以參考一下
    2021-11-11
  • python 計算積分圖和haar特征的實例代碼

    python 計算積分圖和haar特征的實例代碼

    今天小編就為大家分享一篇python 計算積分圖和haar特征的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python 查看list中是否含有某元素的方法

    Python 查看list中是否含有某元素的方法

    今天小編就為大家分享一篇Python 查看list中是否含有某元素的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-06-06
  • Python關(guān)于迭代器的使用

    Python關(guān)于迭代器的使用

    這篇文章主要介紹了Python關(guān)于迭代器的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • Flask創(chuàng)建并運(yùn)行數(shù)據(jù)庫遷移的實現(xiàn)過程

    Flask創(chuàng)建并運(yùn)行數(shù)據(jù)庫遷移的實現(xiàn)過程

    Flask創(chuàng)建并運(yùn)行數(shù)據(jù)庫遷移的過程是一個涉及多個步驟的操作,旨在幫助開發(fā)者在開發(fā)過程中管理數(shù)據(jù)庫模式的變化,而不需要手動地刪除和重建數(shù)據(jù)庫表,從而避免數(shù)據(jù)丟失,以下是一個詳細(xì)的步驟說明,需要的朋友可以參考下
    2024-09-09
  • 基于Python實現(xiàn)自動掃雷詳解

    基于Python實現(xiàn)自動掃雷詳解

    這篇文章主要介紹了如何利用Python+OpenCV實現(xiàn)了自動掃雷,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定的幫助,需要的可以參考一下
    2022-01-01
  • django form和field具體方法和屬性說明

    django form和field具體方法和屬性說明

    這篇文章主要介紹了django form和field具體方法和屬性說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • django 創(chuàng)建過濾器的實例詳解

    django 創(chuàng)建過濾器的實例詳解

    這篇文章主要介紹了django 創(chuàng)建過濾器的實例詳解的相關(guān)資料,主要說明django 創(chuàng)建過濾器來統(tǒng)一處理字符串,需要的朋友可以參考下
    2017-08-08
  • 分析PyTorch?Dataloader報錯ValueError:num_samples的另一種可能原因

    分析PyTorch?Dataloader報錯ValueError:num_samples的另一種可能原因

    這篇文章主要介紹了分析PyTorch?Dataloader報錯ValueError:num_samples的另一種可能原因,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python?ArcPy批量掩膜、重采樣大量遙感影像的操作

    Python?ArcPy批量掩膜、重采樣大量遙感影像的操作

    這篇文章主要介紹了Python?ArcPy批量掩膜、重采樣大量遙感影像,本文介紹基于Python中ArcPy模塊,對大量柵格遙感影像文件進(jìn)行批量掩膜與批量重采樣的操作,需要的朋友可以參考下
    2023-03-03

最新評論