我是 python 新手,需要帮助。我有一个文件,想将文本提取到另一个文件。
输入文件如下所示:
<Datei Kennung="4bc78" Titel="Morgen 1" Bereich="I847YP"> Morgen 1
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
</Datei>
<Datei Kennung="469" Titel="Trop Hall W " Bereich="izr"> Trop Hall W
Here is text, contains numbers and text.
Here is text, contains numbers and text.
</Datei>
对于我文件中的第一个区域,我需要输出包含以下内容的文件 Morgen 1.txt:
Morgen 1
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
Here is text, contains numbers and text.
我从其他用户那里得到了这个代码:
import re
REG_PARSE=re.compile(r'<Datei[^>]*Titel="\s*([^"]*?)\s*"[^>]*>\s*\1\s*(.*?</Datei>',re.dotall)
with open(filename) as infile:
for outfilename, text = REG_PARSE.finditer(infile.read()):
with open('%s.txt'%outfilename,'w') as outf:
outf.write(text)
但它不起作用