Python 刮取返回的空列表索引不是';t空

Python 刮取返回的空列表索引不是';t空,python,list,web-scraping,Python,List,Web Scraping,我从一个网页上抓取数据,当我将数据加载到一个列表中时,它看起来是这样的 [['text', 'text', '', '', 'text', 'text']] >>> a [['text', 'text', '', '', 'text', 'text']] >>> [[y for y in x if y] for x in a] [['text', 'text', 'text', 'text']] >>> 我正在尝试从所有列表中删除空字符

我从一个网页上抓取数据,当我将数据加载到一个列表中时,它看起来是这样的

[['text', 'text', '', '', 'text', 'text']]
>>> a
[['text', 'text', '', '', 'text', 'text']]
>>> [[y for y in x if y] for x in a]
[['text', 'text', 'text', 'text']]
>>> 
我正在尝试从所有列表中删除空字符串,但到目前为止,我尝试的一切都不起作用

results = []
for list in scrape_list:
    for item in scrape_list:
        if item != '':
            results.append(item)



OUTPUT: [['text', 'text', '', '', 'text', 'text']]



scrape_list1 = list(filter(None, scrape_list))
     OUTPUT: [['text', 'text', '', '', 'text', 'text']]``

我想知道这些索引实际上是否不是空字符串,是否包含一个值。如果其他人遇到过这种情况,请随时告诉我发生了什么,因为我无法理解

如果您想要一种纯粹的python方式,可以使用嵌套列表理解

[[y for y in x if y] for x in a]
在我的电脑上,控制台是这样的

[['text', 'text', '', '', 'text', 'text']]
>>> a
[['text', 'text', '', '', 'text', 'text']]
>>> [[y for y in x if y] for x in a]
[['text', 'text', 'text', 'text']]
>>> 

正如@chunjef在评论中所提到的,您在
scrape_list
中迭代了两次。顺便说一句,更紧凑的方式是

>>> ll = [['text', 'text', '', '', 'text', 'text']]
>>> results = [item for l in ll for item in l if item!='']
>>> results
['text', 'text', 'text', 'text']
其中,
[item for l in ll for item in l if item!=''']
都会将您的列表展平
ll
,如果它与空字符串
'

不同,则会删除任何
l
的项目,我猜这只是一个输入错误(如在评论中所述):

scrape_列表
中的单个项目是一个
列表
,而且肯定是
!='',因此此内部列表将附加到
结果
,从而输出。
scrape_list
的嵌套特性也会使筛选语句失败。你可以用

scrape_list1 = [s for l in scrape_list for s in filter(None, l)]

获取一个简单的字符串列表。

您在
scrape\u list
中迭代了两次。