如果有人可以帮助我用python或perl编写脚本,从给定的文件中检索所有的句子,例如:
[LANG::...]
Run Code Online (Sandbox Code Playgroud)
对于ecxample:
[LANG::Sample text with digits 0123]
Run Code Online (Sandbox Code Playgroud)
并将其写入单行文件。
非常感谢您的帮助
编辑:
感谢您的帮助,现在更高级了。
如果发现类似[:ANG :: ...]的内容,请只写...,不带括号ang LANG ::标记。
谢谢你们,你真棒:)
import re
with open('input.txt', 'w') as f:
text = f.read()
#text = 'Intro [LANG::First text 1] goes on [LANG::Second text 2] and finishes.'
with open('output.txt', 'w') as f:
for match in re.findall('\[LANG::.*?\]', text):
f.write(match+'\n')
Run Code Online (Sandbox Code Playgroud)
输出:
[LANG::First text 1]
[LANG::Second text 2]
Run Code Online (Sandbox Code Playgroud)
问题的第二部分:如果发现类似[:ANG :: ...]的内容,请只写...,不带方括号和LANG ::标记。
将最后一部分更改为:
with open('output.txt', 'w') as f:
for match in re.findall('\[.ANG::.*?\]', text):
if match.startswith('[:ANG'):
f.write(match[7:-1]+'\n')
else:
f.write(match+'\n')
Run Code Online (Sandbox Code Playgroud)
match[7:-1]根据您的需要修复该子字符串部分。