gpt4 book ai didi

python - 如何从 Python 中的流(不是磁盘支持的文件)中读取 Excel 文件?

转载 作者:太空狗 更新时间:2023-10-29 13:32:49 24 4
gpt4 key购买 nike

XLRD 已安装并测试:

>>> import xlrd
>>> workbook = xlrd.open_workbook('Sample.xls')

当我通过如下 html 表单读取文件时,我能够访问所有值。

  xls_file = request.params['xls_file']
print xls_file.filename, xls_file.type

我正在使用 Pylons 模块,请求来自:from pylons import request, tmpl_context as c

我的问题:

  1. 通过requst.params读取的xls_file是一个对象吗?
  2. 如何读取 xls_file 并使其与 xlrd 一起工作?

更新:

xls_file 已上传到 Web 服务器,但 xlrd 库需要一个文件名而不是打开的文件对象,如何使上传的文件与 xlrd 一起使用? (感谢 Martijn Pieters,我无法清楚地提出问题。)

最佳答案

xlrd 确实支持直接提供数据而无需文件路径,只需使用 file_contents 参数即可:

xlrd.open_workbook(file_contents=fileobj.read())

来自documentation :

file_contents – A string or an mmap.mmap object or some other behave-alike object. If file_contents is supplied, filename will not be used, except (possibly) in messages.

关于python - 如何从 Python 中的流(不是磁盘支持的文件)中读取 Excel 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11955300/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com