[英]How to parse the line that has a specific word in it?
I'm letting my python code go through an HTML document and while it does, I need it to find a specific words and then, parse the lines that have the following words我让我的 python 代码通过一个 HTML 文档,当它这样做时,我需要它来查找特定的单词,然后解析具有以下单词的行
For example例如
if HTML document looks like this如果 HTML 文档看起来像这样
htmlDocument = '''
word 023-213103-2402131025901238923213
bla bla bla
bla bla bla
word 2512-521-096-07464325
bla bla bla
bla bla bla
word 123123-0293231
'''
I need my desirableList to look like this after parsing解析后我需要我的 desiredList 看起来像这样
desirableList = [
"word 023-213103-2402131025901238923213",
"word 2512-521-096-07464325",
"word 123123-0293231"
]
Here's one way:这是一种方法:
>>> desirableList = [s for s in htmlDocument.split("\n") if "word" in s]
>>> desirableList
['word 023-213103-2402131025901238923213', 'word 2512-521-096-07464325', 'word 123123-0293231']
Update the conditional, as needed, to get other kinds of results like "line starts with":根据需要更新条件以获得其他类型的结果,例如“行开头为”:
[s for s in htmlDocument.split("\n") if s.startswith("word")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.