删除python列表中的None

删除python列表中的None,python,excel,openpyxl,Python,Excel,Openpyxl,Q1. 我希望获得以下输出: [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.ge

Q1.

我希望获得以下输出:

[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)]

问题2。在相同的代码中,如果我想通过索引而不是openpyxl中的名称来获取工作表,该怎么办。我该怎么做呢?

您可以这样过滤:

s = ['Randy', 'None', 'None', 'Shaw', 'None', 'None']

s = [i for i in s if i != "None"]
请记住,列表中有字符串
“None”
,而不是对象
None

第二季度:

workbook = load_workbook('C:/Users/Desktop/test.xlsx')

sheet = wworkbook.worksheets[0] #here, either loop over or access by whatever index you want.

“无”值不是文本
none
类型值。他们的弦。因此,您需要根据字符串测试它们。例如:

>>> lst = ['Randy', 'None', 'None', 'Shaw', 'None', 'None']
>>> [el for el in lst if el != 'None']
['Randy', 'Shaw']
>>>

因为这是字符串
“None”
而不是单一的、非类型的对象
None
可能的Hi副本,这很有效。谢谢!但是第二季度呢?有办法吗?