python 读取txt 输出指定内容

例子:
17 if 18 some-time “today like it” stop close start like it
19 20 21 if some-time “like it is” stop my close start is here
12 if 16 13 some-time “do it” go to close start yes

要求:
删除 some和star中间的字符,输出保存到txt

结果:
17 if 18 like it
19 20 21 if is here
12 if 16 13 yes

代码如下

a = """17 if 18 some-time “today like it” stop close start like it

19 20 21 if some-time “like it is” stop my close start is here

12 if 16 13 some-time “do it” go to close start yes"""

import re

for i in a.split('\n'):

    # print(i)

    get_data = re.findall("(.*)if .*? “(.*?)” .*",i)

    print(get_data[0][0],get_data[0][1])


温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-09-17
import re

text = open('..\...\***旧文本路径***old.txt','r').read()
new_text= re.sub('some.*start','',text)
with open('..\...\***新保存文本路径***new.txt','w') as f:
f.write(new_text)