我有一个脚本,可以使用 xlrd 模块(特别是 row_values() 方法)从 Excel 电子表格中提取数据。它似乎做得很好,除了以前的 VLookups 自动生成的“#N/A”,在这种情况下,xlrd 将“#N/A”获取为整数 42。
我查看了字符串格式化方法,但是不明白这是怎么回事。
除了有一个发现生命意义的脚本(42)之外,有人能提出问题可能是什么吗?
干杯
注意:该工作表中不再包含 Vlookups,所有值都已从其他工作表复制,一切都是纯值,没有公式。
I have a script that pulls data out of an excel spreadsheet using the xlrd module, specifically the row_values() method. It appears to do a great job, except for where "#N/A" has been auto-generated by previous VLookups, in which case xlrd gets "#N/A" as integer 42.
I had a look at string formatting methods but couldn't see how that was the issue.
Apart from having a script that has discovered the meaning of life (42), can anyone suggest what the issue may be?
Cheers
Note: The sheet no longer has the Vlookups in it, all values have been copied from other sheets, everything is plain values, no formulas.
发布评论
评论(4)
我发现这很有用。感谢约翰最初的帮助。
I found this useful. Thanks to John's initial help.
网络上的 xlrd 文档(或在您的计算机上;在浏览器中打开文档并执行
Ctrl-F #N/A
)为您提供 Excel 内码到文本的转换表。查看 sheet.row_types() 可能有用方法,以及 Cell 类文档为您提供由sheet.row_types()和其他返回的类型号之间的交叉引用。请注意,测试这些类型编号通常比对值使用 isinstance() 更有效,并且使用类型编号不会产生歧义。
xlrd docs on the web (or on your computer; open the docs in your browser and do
Ctrl-F #N/A
) give you the conversion table from Excel internal codes to text.It might be of use to look at the sheet.row_types() method, and the Cell class docs which give you the cross-reference between type numbers as returned by sheet.row_types() and others. Note that it is generally more efficient to test for those type numbers than it is to use isinstance() on the values, AND there is no ambiguity using type numbers.
正如 Andrew 所列,如果单元格中有错误,xlrd 会写入错误代码,您可以看到 此处:
将代码 0x2A 从十六进制转换为十进制,您可以获得 42 值。
为了避免这种情况,您可以在代码中使用类似的内容:
As Andrew listed if you have error in the cell, xlrd writes the code of the error, which you can see here:
Converting code 0x2A from hex to dec you can get that 42 value.
To avoid that you can use something like this in your code:
根据单元格类型识别错误单元格。
简单的解决方案可能是识别错误单元格并输入“无”而不是 42
XLRD 文档:
您可以识别所有另一种类型基于以下文档
XL_CELL_ERROR 5 int 表示内部 Excel 代码;对于文本表示,请参阅提供的字典 error_text_from_code
Identify Error cell based on cell type.
Simple solution might be Identify Error cell and put 'None' instead of 42
XLRD Documentation:
You can identify the all the other type based on below documentation
XL_CELL_ERROR 5 int representing internal Excel codes; for a text representation, refer to the supplied dictionary error_text_from_code