3 回答
TA贡献1850条经验 获得超11个赞
您可以使用正则表达式来检查这一点。一种可能的解决方案是:
id1 = "I"
id2 = "need"
regex = re.compile(r'^.*{}.*{}.*$'.format(id1, id2))
with open('fun.txt') as f:
for line in f:
if re.search(regex, line):
print(line[:-1])
TA贡献1744条经验 获得超4个赞
您需要确定id2在该行的部分后 id1:
infile = [
"This is a long drawn out sentence needed to emphasize a topic I am trying to learn.",
"It is new idea for me and I need your help with it please!",
"Thank you so much in advance, I really appreciate it.",
]
id1 = "I"
id2 = "need"
for line in infile:
if id1 in line:
pos1 = line.index(id1)
if id2 in line[pos1+len(id1) :] :
print(line)
输出:
It is new idea for me and I need your help with it please!
添加回答
举报