私は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)
しかし、それは機能しません