我想提取一行中关键字旁边的单词。 注意:该关键字在该行中存在两次,我想在关键字旁边打印两个单词。
示例:
This is my first Python language and I like this language to the most.
所需输出:(需要以下格式language
旁边的字词)
and
to
答案 0 :(得分:0)
re.findall
具有零宽度正向后观((?<=language )
)以匹配language
之前,然后使用\w+
和print
获取所需的子字符串sep='\n'
获得所需的输出格式:
In [33]: s = 'This is my first Python language and I like this language to the most.'
In [34]: print(*re.findall(r'(?<=language )\w+', s), sep='\n')
and
to
答案 1 :(得分:0)
data = 'This is my first Python language and I like this language to the most.'
for i, word in enumerate(data.split()):
if word == 'language':
print(data.split()[i + 1])
答案 2 :(得分:0)
text = "hola esto es una prueba donde esto busca la siguiente linea de esto"
brk = "esto"
for i, texto in enumerate(text.split(brk)):
if i > 0:
print( texto.split()[0] if len(texto.split()) > 0 else '')
现在你只需要改变文本和中断工作