我有一个脚本,它使用 xlrd 模块从 Excel 电子表格中提取数据,特别是 row_values() 方法。它似乎做得很好,除了以前的 VLookups 自动生成“#N/A”的地方,在这种情况下,xlrd 将“#N/A”作为整数 42 获取。
我查看了字符串格式化方法,但看不出这是怎么回事。
除了有一个发现生命意义的脚本(42)之外,任何人都可以提出问题可能是什么?
干杯
注意:工作表中不再有 Vlookups,所有值都从其他工作表复制,一切都是普通值,没有公式。
我发现这很有用。感谢约翰最初的帮助。
def xls_proc_text(cell, value_proc=None, text_proc=None):
"""Converts the given cell to appropriate text."""
"""The proc will come in only when the given is value or text."""
ttype = cell.ctype
if ttype == xlrd.XL_CELL_EMPTY or ttype == xlrd.XL_CELL_TEXT or ttype == xlrd.XL_CELL_BLANK:
if text_proc is None:
return cell.value
else:
return text_proc(cell.value)
if ttype == xlrd.XL_CELL_NUMBER or ttype == xlrd.XL_CELL_DATE or ttype == xlrd.XL_CELL_BOOLEAN:
if value_proc is None:
return str(cell.value)
else:
return str(value_proc(cell.value))
if cell.ctype == xlrd.XL_CELL_ERROR:
# Apply no proc on this.
return xlrd.error_text_from_code[cell.value]
网络上的 xlrd 文档(或在您的计算机上;在浏览器中打开文档并执行Ctrl-F #N/A
)为您提供从 Excel 内部代码到文本的转换表。
查看sheet.row_types() 方法和Cell 类文档可能很有用,它们为您提供 sheet.row_types() 和其他返回的类型编号之间的交叉引用。请注意,测试这些类型号通常比在值上使用 isinstance() 更有效,并且使用类型号没有歧义。
正如 Andrew 所列出的,如果单元格中有错误,xlrd 会写入错误代码,您可以在此处看到:
0x00: '#NULL!', # Intersection of two cell ranges is empty
0x07: '#DIV/0!', # Division by zero
0x0F: '#VALUE!', # Wrong type of operand
0x17: '#REF!', # Illegal or deleted cell reference
0x1D: '#NAME?', # Wrong function or range name
0x24: '#NUM!', # Value range overflow
0x2A: '#N/A', # Argument or function not available
将代码 0x2A 从十六进制转换为十进制,您可以获得 42 值。为避免这种情况,您可以在代码中使用类似的内容:
for rownum in xrange(sh.nrows):
wr.writerow(['#N/A' if col.ctype == xlrd.XL_CELL_ERROR else col.value for col in sh.row(rownum)])
简单的解决方案可能是识别错误单元格并输入“无”而不是 42
textType = sheet.cell(r,0).ctype #Get the type of the cell
if textType == 5:
text = None
else:
text = sheet.cell(r, 0).value
XLRD文档:
您可以根据以下文档识别所有其他类型
XL_CELL_ERROR 5 int 代表内部 Excel 代码;有关文本表示,请参阅提供的字典 error_text_from_code