1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python Excel 操作 | xlrd+xlwt 模块笔记

Python Excel 操作 | xlrd+xlwt 模块笔记

时间:2023-11-29 22:22:24

相关推荐

Python Excel 操作 | xlrd+xlwt 模块笔记

Python 的pandas模块使用xlrd作为读取 excel 文件的默认引擎。但是,xlrd在其最新版本(从 2.0.1 版本开始)中删除了对 xls 文件以外的任何文件的支持。

xlsx files are made up of a zip file wrapping an xml file.

Both xml and zip have well documented security issues, which xlrd was not doing a good job of handling. In particular, it appeared that defusedxml and xlrd did not work on Python 3.9, which lead people to uninstall defusedxml as a solution, which is absolutely insane, but then so is sticking with xlrd 1.2 when you could move to openpyxl.

从官方的邮件中,说的应该是 xlsx 本身是由一个 zip 文件和 xml 的头文件构成的,但是 xml 和 zip 都有详细记录的安全问题,特别是,defusedxmlxlrd似乎在 Python 3.9 上不起作用,这导致人们卸载defusedxml作为解决方案,这绝对是疯了,但是,当然了,您也可以转移到openpyxl,或者仍然坚持使用xlrd 1.2

$condasearchxlrd

Loadingchannels:done

#NameVersionBuildChannel

xlrd1.0.0py27_0conda-forge

xlrd1.0.0py27_1conda-forge

xlrd1.0.0py35_0conda-forge

xlrd1.0.0py35_1conda-forge

xlrd1.0.0py36_0conda-forge

xlrd1.0.0py36_1conda-forge

xlrd1.1.0py27_1pkgs/main

xlrd1.1.0py27ha77178f_1pkgs/main

xlrd1.1.0py35_1pkgs/main

xlrd1.1.0py35h45a0a2a_1pkgs/main

xlrd1.1.0py36_1pkgs/main

xlrd1.1.0py36h1db9f0c_1pkgs/main

xlrd1.1.0py37_1pkgs/main

xlrd1.1.0py_2conda-forge

xlrd1.2.0py27_0pkgs/main

xlrd1.2.0py36_0pkgs/main

xlrd1.2.0py37_0pkgs/main

xlrd1.2.0py_0conda-forge

xlrd1.2.0py_0pkgs/main

xlrd1.2.0pyh9f0ad1d_1conda-forge

xlrd2.0.1pyhd3eb1b0_0pkgs/main

xlrd2.0.1pyhd8ed1ab_3conda-forge

上面的问题将导致您在使用pandas调用 xlsx excel 上的read_excel函数时收到一个错误,即不再支持 xlsx filetype。

为了解决这个问题,你可以: 安装 openpyxl 模块:这是另一个仍然支持 xlsx 格式的 excel 处理包。

pandas中把默认的 engine 由原来的xlrd替换成openpyxl

#Installopenyxl

pipinstallopenpyxl

#setengineparameterto"openpyxl"

pd.read_excel(path,engine='openpyxl')

接下来,介绍一下Python 读写 Excel 需要导入的xlrd(读),xlwd(写)模块的一些常用操作。

1. xlrd 模块

1.1 Excel 文件处理

打开 excel 文件

importxlrd

excel=xlrd.open_workbook("data.xlsx")

获取并操作 sheet 工作表

sheet_names=excel.sheet_names() #返回book中所有工作表的名字,['Sheet1','Sheet2','Sheet3']

excel.sheet_loaded(sheet_nameorindx)#检查某个sheet是否导入完毕

#以下三个函数都会返回一个xlrd.sheet.Sheet()对象

sheet=excel.sheet_by_index(0)#通过索引获取,例如打开第一个sheet表格

sheet=excel.sheet_by_name("sheet1")#通过名称获取,如读取sheet1表单

sheet=excel.sheets()[0]#通过索引顺序获取

sheet.row_values(0)#获取第一行的数据

sheet.col_values(0)#获取第一列的数据

sheet.nrows #获取总共的行数

sheet.ncols #获取总共的列数

遍历所有行

foriinrange(0,sheet.nrows):

row_list=sheet.row_values(i)#每一行的数据在row_list数组里

1.2 日期处理

importdatetime

fromxlrdimportxldate_as_datetime

xldate_as_datetime(43346.0,0).strftime('%Y/%m/%d')

#'/09/03'

2. xlwt 模块

2.1 创建 Book 工作簿(即 excel 工作簿)

importxlwt

workbook=xlwt.Workbook(encoding='utf-8')#创建一个workbook并设置编码形式

2.2 添加 sheet 工作表

worksheet=workbook.add_sheet('MyWorksheet')#创建一个worksheet

2.3 向工作表中添加数据并保存

worksheet.write(1,0,label='thisistest')#参数对应行,列,值

workbook.save('save_excel.xls')#保存

Python 中常见的 TypeError 是什么?

-04-16

Python 列表、字典、元组的一些小技巧

-03-30

如何卸载 python setup.py install 安装的包?

-05-15

生物信息学 Python 入门之源码安装

-09-29

Python 文件与目录操作方法总结

-02-17

本文分享自微信公众号 - 生信科技爱好者(bioitee)。

如有侵权,请联系 support@ 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。