![](/img/trans.png)
[英]Remove entire line in text file that contain with specific word/string in python
[英]Python:Want to remove the line that contain specific word
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”一詞的行,因為我不想一一刪除。 有什么線索怎么做?
您可以這樣操作:
stocks = [s for s in stocks if 'berhad' not in s.lower()]
假設股票只是通常的清單,您可以嘗試類似
trimmed_stocks = [ x for x in stocks if not 'Berhad' in x ]
從您的帖子尚不清楚,例如,是否也應該排除BERHAD或bErHaD,但是可以類似地處理它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.