Python这样操作能存储100多万行的xlsx文件!Python让你事半功倍!

(1) 如果excel文件是xls,2003版的,使用xlrd和xlwt库来对xls文件进行操作

(2) 如果excel文件是xlsx,2007以上版的,使用openpyxl库来对xlsx文件进行操作

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>Tips:xlrd、xlwt和openpyxl非python自带库,需要进行安装,可以查看我之前的文章。
</pre>

如果你在学习Python的过程当中有遇见任何问题,可以加入我的python交流学习qq群:683380553,多多交流问题,互帮互助,群里有不错的学习教程和开发工具。学习python有任何问题(学习方法,学习效率,如何就业),可以随时来咨询我,如果你准备学习大数据,也欢迎加入大数据学习交流qq群683380553,每天与大家分享学习资源哦。

我们使用Python做数据挖掘和分析时候,当数据行超过一定数量,xls文件是存不下的。显然无法满足我们的大量数据存储需求,这个时候需要改用xlsx。

那具体xls和xlsx最大分别可以存多少行呢?

(1) 对于2003版本的xls文件,最大行数是65536行

(2) 对于2007版本的xlsx文件,最大行数是1048576行

闲话不多聊,直接上代码干货

<pre spellcheck=”false” style=”box-sizing: border-box; margin: 5px 0px; padding: 5px 10px; border: 0px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-weight: 400; font-stretch: inherit; font-size: 16px; line-height: inherit; font-family: inherit; vertical-align: baseline; cursor: text; counter-reset: list-1 0 list-2 0 list-3 0 list-4 0 list-5 0 list-6 0 list-7 0 list-8 0 list-9 0; background-color: rgb(240, 240, 240); border-radius: 3px; white-space: pre-wrap; color: rgb(34, 34, 34); letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>#!/usr/bin/env python3

– coding: utf-8 –

读写2007 excel

import pprint
import openpyxl
import openpyxl.styles
from openpyxl.styles import Font,colors

读取Excel文件

def readExcel(filename):
workbook = openpyxl.load_workbook(filename)
worksheet = workbook[‘用户’]
list = []
for row in worksheet.rows: # 1行1行读
lineData = [] # 行数据
col = 1
for cell in row:
lineData.append(cell.value) # 1列1列读
col = col + 1
list.append(lineData)
return list

操作数据

def operateData(filename):
list = readExcel(filename)
# 去除第一行,第一行是表头
list.pop(0)
pprint.pprint(list)
pprint.pprint(“先按性别排序,再按身高排序”)
# 要对身高进行排序,但是男女有别
# 先按性别排序,再按身高排序
list.sort(key=lambda ele: (ele[3], ele[4]))
pprint.pprint(list)

获取Excel标签列表

比如第5行标签列表,总共4列 [‘A5’, ‘B5’, ‘C5’, ‘D5’]

def getTagList(index, colNum):
tagList = []
for i in range(0, colNum):
# A的ascii码值65
tag = chr(65 + i) + str(index)
tagList.append(tag)
return tagList

写入Excel文件

def writeExcel(outputFilePath, list):
book = openpyxl.Workbook()
sheet = book.create_sheet(“用户”, 0)
sheet.title = “用户” # sheet名称
rowNum = len(list)
try:
# 1行1行读取
for i in range(1, rowNum + 1): # 下标从1开始
datalist = list[i -1] # 读取1行
col = 1
# 1列1列写入
for data in datalist:
sheet.cell(i, col, data) #写入内容
col += 1
# 获取标签列表
tagList = getTagList(i, len(datalist))
font = Font(‘微软雅黑’, size = 14, color = ‘333333’)
# 设置单元格字体、字号、颜色
for tag in tagList:
sheet[tag].font = font # 设置字体
# 保存文件
book.save(outputFilePath)
except Exception as e: # 捕获异常
print(e)

主函数

if name == “main“:
print(“读取xlsx格式的数据”)
userList = readExcel(‘user.xlsx’)
print(“写入xlsx文件”)
writeExcel(“user2.xlsx”, userList)
print(“操作数据”)
operateData(‘user.xlsx’)
</pre>

<tt-image data-tteditor-tag=”tteditorTag” contenteditable=”false” class=”syl1555312098291 ql-align-center” data-render-status=”finished” data-syl-blot=”image” style=”box-sizing: border-box; cursor: text; text-align: left; color: rgb(34, 34, 34); font-family: “PingFang SC”, “Hiragino Sans GB”, “Microsoft YaHei”, “WenQuanYi Micro Hei”, “Helvetica Neue”, Arial, sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: pre-wrap; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255); text-decoration-style: initial; text-decoration-color: initial; display: block;”>
《Python这样操作能存储100多万行的xlsx文件!Python让你事半功倍!》 image

<input class=”pgc-img-caption-ipt” placeholder=”图片描述(最多50字)” value=”” style=”box-sizing: border-box; outline: 0px; color: rgb(102, 102, 102); position: absolute; left: 187.5px; transform: translateX(-50%); padding: 6px 7px; max-width: 100%; width: 375px; text-align: center; cursor: text; font-size: 12px; line-height: 1.5; background-color: rgb(255, 255, 255); background-image: none; border: 0px solid rgb(217, 217, 217); border-radius: 4px; transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1) 0s;”></tt-image>

    原文作者:不谈风月_0eb8
    原文地址: https://www.jianshu.com/p/b4d68250c789
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞