Openpyxl - 无法从列表中删除 'none' 个单元格值
Openpyxl - Unable to delete 'none' cell values from list
我正在使用 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)
这解决了我的问题
我正在使用 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)
这解决了我的问题