如何从python列表中删除u'\ n \ n \ n \ n \ n \ n \ n \ n'和'u'\ xa0'

时间:2015-07-24 03:59:34

标签: python list

我已经挣扎了两天但是无法理解。这是我的代码:

def find_name():
    i = 0 
    while i != len(links):
        url = links[i]
        r = requests.get(url)
        html = r.content
        soup = BeautifulSoup(html)
        for n in soup.find_all('tr'):
            td = n.find('td')
            if td: 
                last_name.append(td.text)
        i = i+1 
    del last_name[0:5]
    return last_name

它会生成一个姓氏列表,但是我希望它们消失的列表中有多个u'\ xa0'和'/ u'\ n \ n \ n \ n \ n。我尝试了我所知道的一切。就像通过检查每个元素但它给我值错误list.remove(x):x不在列表中,我也尝试将每个元素比较为 - u'\ n \ n \ n \ n \ n \ n \ n \ n \ n'然后添加到列表中。但它没有用。 stackoverflow还有其他问题,但他们都谈论字符串。

2 个答案:

答案 0 :(得分:2)

在将文本添加到last_name列表之前,您可以在文本上调用str.strip()

          if td and td.text.strip(): 
              last_name.append(td.text)

答案 1 :(得分:1)

您可以使用列表推导和strip方法:

# Your code
last_name = [name for name in last_name if name.strip()]
return last_name