pyExcelerator 或 xlrd - 如何查找/搜索给定几列数据的行?
Python 与 EXCEL 通信...我需要找到一种方法,以便我可以查找/搜索给定列数据的行。 现在,我逐一扫描整个行...这将很有用,如果有一些功能,如查找/搜索/替换...我在 pyExcelerator 或 xlrd 模块中没有看到这些功能..我不想使用 win32com模块! 它使我的工具基于 Windows!
通过 Python 查找/搜索 Excel 行...有人知道吗?
Python communicating with EXCEL... i need to find a way so that I can find/search a row for given column datas. Now, i m scanning entire rows one by one... It would be useful, If there is some functions like FIND/SEARCH/REPLACE .... I dont see these features in pyExcelerator or xlrd modules.. I dont want to use win32com modules! it makes my tool windows based!
FIND/SEARCH Excel rows through Python.... Any idea, anybody?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
@John Fouhy:[我是 xlwt 的维护者,也是 xlrd 的作者]
pyExcelerator 的电子表格读取部分已被严重弃用,以至于它完全从 xlwt 中消失。 要使用 Python 2.1+ 读取 Excel 2.0 至 11.0 (Excel 2003) 或兼容软件创建的任何 XLS 文件,请使用 xlrd
xlrd 不需要“简单优化”:
@John Fouhy: [I'm the maintainer of xlwt, and author of xlrd]
The spreadsheet-reading part of pyExcelerator was so severely deprecated that it vanished completely out of xlwt. To read any XLS files created by Excel 2.0 up to 11.0 (Excel 2003) or compatible software, using Python 2.1+, use xlrd
That "simple optimi[sz]ation" isn't needed with xlrd:
“现在,我逐行扫描整行”
这有什么问题吗? 在电子表格环境中,“搜索”确实很复杂。 搜索值? 搜索公式? 向下搜索行然后跨列搜索? 只搜索特定列? 只搜索特定行?
电子表格不是简单的文本——简单的文本处理设计模式不适用。
电子表格搜索很困难,而您做得正确。 没有什么比这更好的了,因为它很难。
"Now, i m scanning entire rows one by one"
What's wrong with that? "search" -- in a spreadsheet context -- is really complicated. Search values? Search formulas? Search down rows then across columns? Search specific columns only? Search specific rows only?
A spreadsheet isn't simple text -- simple text processing design patterns don't apply.
Spreadsheet search is hard and you're doing it correctly. There's nothing better because it's hard.
你不能。 这些工具不提供搜索功能。 您必须循环迭代数据并自行搜索。 对不起。
You can't. Those tools don't offer search capabilities. You must iterate over the data in a loop and search yourself. Sorry.
使用 pyExcelerator,您可以通过首先查找最大行索引和列索引(并存储它们)来进行简单的优化,以便您可以迭代
(row, i) for i in range(maxcol+1)
迭代所有字典键。 这可能是您得到的最好的结果,除非您想遍历并构建字典映射值到键集。顺便说一句,如果您使用 pyExcelerator 编写电子表格,请注意它有一些错误。 我遇到过一个涉及写入 230 和 232(或附近)之间的整数的情况。 如今,显然很难联系到原作者,因此 xlwt 是一个修复(已知)错误的分支。 对于编写电子表格,它是 pyExcelerator 的直接替代品; 您可以执行
import xlwt as pyExcelerator
并且不做其他任何更改。 但它不读取电子表格。With pyExcelerator you can do a simple optimization by finding the maximum row and column indices first (and storing them), so that you iterate over
(row, i) for i in range(maxcol+1)
instead of iterating over all the dictionary keys. That may be the best you get, unless you want to go through and build up a dictionary mapping value to set of keys.Incidentally, if you're using pyExcelerator to write spreadsheets, be aware that it has some bugs. I've encountered one involving writing integers between 230 and 232 (or thereabouts). The original author is apparently hard to contact these days, so
xlwt
is a fork that fixes the (known) bugs. For writing spreadsheets, it's a drop-in replacement for pyExcelerator; you could doimport xlwt as pyExcelerator
and change nothing else. It doesn't read spreadsheets, though.