我有一个包含数据的 Excel 文件。我感兴趣的列(A 列)包含空白单元格、单个字符“Y”、单词“Runmode”或我感兴趣的唯一值。
我只想提取唯一值并将其打印到控制台,但遇到了麻烦。
我正在使用 openpyxl 来解析 .xlsx 文件。
wb = load_workbook(location + '\\' + suite)
data_sheet = wb['Data']
for i in range(1, data_sheet.max_row + 1):
cell_value = data_sheet['A' + str(i)].value
if cell_value is not 'Y' and cell_value is not 'Runmode' and cell_value is not None:
print(cell_value == 'Runmode')
print(repr(cell_value), cell_value, 'success')
输出正在打印“Runmode”,即使它等于 True 等于我告诉脚本忽略的值。我错过了什么吗?
最佳答案
您应该使用 ==
测试字符串是否相等,就像您在 print 语句中所做的那样。或者至少你应该做出打印声明:
print(cell_value is 'Runmode')
确保您正在检查相同的内容。
is
和 ==
之间的这种差异例如如果 cell_value
是 unicode 并且您使用的是 Python 2.7,就会发生这种情况。
正如 Steven Rumbalski 指出的那样,您最好将 if
语句重写为:
if cell_value not in ('Runmode', 'Y', None):
关于python - 从 .xlsx 文件打印唯一值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40837686/