【发布时间】:2020-07-15 21:17:25
【问题描述】:
我正在尝试编写一个脚本来在特定字符串之后打印特定单词。
这是输入文件
Theyare "playing in the ground", with friends
Theyare "going to Paris", with family
Theyare "motivating to learn new things", by themselves
在输出中,我尝试选择“are”作为关键字,在“are”之后,我想要“”中的文本,并且我想将空格前的文本添加到“”。
输出应该是
They playing in the ground
They going to Paris
They motivating to learn new things
我可以使用以下代码打印该行的其余部分,但不能打印某些单词。到目前为止我有
with open ('input.txt', 'r') as f:
for lines in f:
a = re.search(r'\bare', f):
if a:
print (lines)
任何帮助将不胜感激
【问题讨论】:
标签: python-3.x regex