我正在使用 openpyxl 来获取特定列中单元格的值。我在用
load_workbook(path, data_only=True)
因为有些单元格中有公式。
但是,对于 excel 中为空的单元格,我很困惑如何从我的列表中删除它们或首先不附加它们。
这是因为我无法定位这些空单元格。实际上,使用 type() 似乎这些单元格以字符串形式返回(而不是 NoneType)。我试过使用 None、""、"none" 来跳过这些值的附加,但没有成功。
到目前为止,这是我的代码:
import openpyxl
import json
import requests
mypath = "XXX/XXXXXX/XXXXXX/XXX"
filename = "file"
def get_column_number(ws, header_name, max_header_row):
for index, row in enumerate(ws, start=1):
if index > max_header_row:
break
for cell in row:
if cell.value:
if str(cell.value).rstrip() == header_name:
return cell.col_idx
def get_location_list(wb,ws):
location_list = []
for row in range(2, ws.max_row):
row = row + 1
cell_value = str(ws.cell(row=row, column=get_column_number(ws, "Location literal", 1)).value).lower()
print(cell_value)
if cell_value is None:
print("blank")
pass
else:
location_list.append(cell_value)
print(location_list)
for i in list_of_fdims:
wb = openpyxl.load_workbook(mypath+filename+".xlsx", data_only=True)
ws = wb['test_cases']
print(i)
get_location_list(wb,ws)
以下是返回空单元格值的方式:
在列表中:
['none']
使用 print() :
none
如果有人可以帮助我,我将不胜感激。非常感谢您提前!
经过一些试验和错误后,我只需要输入:
if cell_value == "none":
continue
else:
location_list.append(cell_value)
这解决了我的问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句