Python里调用xlrd模块打开Excel表后,怎么关闭?

Zhao_Linda 2016-12-12 02:59:08
在Python里面对Excel表的读取是调用xlrd模块,但是当调用后会去打开Excel文件,在读取完数据之后需要将其关闭吗?如果要,怎么关闭?
例如:
import xlrd
workbook = xlrd.open_workbook("../Resource/TestData/Internal Web Service.xlsx")
worksheet = workbook.sheet_by_name("Sheet1")
......
...全文
5744 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunkaivg 2018-12-18
  • 打赏
  • 举报
回复
有人说不用关闭(参考:https://www.cnblogs.com/zanjiahaoge666/p/7125555.html)

查看官网API,有个release_resources()函数关闭模板文件。(参考:https://xlrd.readthedocs.io/en/latest/on_demand.html?highlight=close)
When using on-demand, it is advisable to ensure that Book.release_resources() is always called, even if an exception is raised in your own code; otherwise if the input file has been memory-mapped, the mmap.mmap object will not be closed and you will not be able to access the physical file until your Python process terminates. This can be done by calling Book.release_resources() explicitly in the finally part of a try/finally block.

The Book object is also a context manager, so you can wrap your code in a with statement that will make sure underlying resources are closed.

ckc 2017-12-15
  • 打赏
  • 举报
回复
找了一下确实没有关闭的函数,只看到release_resources 而且打开之后检查了文件并没有被打开 怀疑在打开的时候把数据全部读到内存中了 所以就不需要关闭了 也许你可以试验一下打开一个巨大的xls文件,看一下是不是python占用了大量的内存,如果是这样,那应该就没错了
wxiaocai1298 2017-12-14
  • 打赏
  • 举报
回复
同问楼主的问题

3,422

社区成员

发帖
与我相关
我的任务
社区描述
其他开发语言 其他开发语言
社区管理员
  • 其他开发语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧