Python:想要删除包含特定单词的行

时间:2016-03-01 14:52:47

标签: python tree lxml

from lxml import html
import requests
import csv
page = requests.get('http://www.google.com/finance?q=[%28exchange+%3D%3D+%22ABC%22%29]&restype=company&noIL=1&start=0&num=1500')
tree = html.fromstring(page.content)

#Scrape stocks companies and symbols

stocks = tree.xpath('//a [not(@class)][@id][@href]/text()')
#This will create a list of prices
stocks.remove('IRM Group Berhad');
stocks.remove('A & M Realty Berhad');
stocks.remove('BERJAYA FOOD BERHAD- A SHARES');


print 'Stocks= ', stocks

# open a file for writing.
csv_out = open('KLSE.csv', 'wb')

mywriter = csv.writer(csv_out)

rows = zip(stocks)
mywriter.writerows(rows)

csv_out.close()

我想删除包含“Berhad”一词的所有行,因为我不想逐一删除它。有任何线索怎么做?

2 个答案:

答案 0 :(得分:1)

你可以这样做:

stocks = [s for s in stocks if 'berhad' not in s.lower()]

答案 1 :(得分:0)

假设股票只是一个通常的列表,你可以尝试像

这样的东西
no tty present

从你的帖子中不清楚是否应该排除BERHAD或者bErHaD,但这些可以被类似地处理。