Q1。
在python列表中刪除無
我想輸出:
[Randy, Shaw]
輸出類似於
['Randy', 'None', 'None', 'Shaw', 'None', 'None']
但它不刪除 '無' 的值。
這裏是我的代碼..
from openpyxl import load_workbook
workbook = load_workbook('C:/Users/Desktop/test.xlsx')
print(workbook.get_sheet_names())
sheet=workbook.get_sheet_by_name('marks')
iterheaders = iter(sheet[1])
headers = [str(cell.value) for cell in iterheaders if cell is not None and cell != '']
Keyword_list = list(filter(None, headers))
我甚至嘗試以下,但它沒有工作,要麼:
Keyword_list = [i for i in headers if (None) != len(i)]
Q2。在相同的代碼中,如果我想通過它的索引而不是它的名字在openpyxl中獲取表單,該怎麼辦?我怎麼做?
,因爲這是*字符串*'「無」'不是單例,NoneType對象'None' –
的可能的複製(https://開頭stackoverflow.com/questions/1157106/remove-all-occurrences-of-a-value-from-a-list) – yabk