我是python中的新成员,需要帮助。 我有一个文件,并希望提取文本到另一个文件。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>
对于我的文件中第一个区域,我需要为摩根的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)
但它不起作用
使用['lxml.etree'(http://lxml.de/)读取INFILE,因为它似乎是XML格式。然后使用普通的[file-io](http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files)写入另一个文件。此外,你到目前为止尝试过什么? – inspectorG4dget
老兄,请编辑您的问题,而不是发表评论与代码 – inspectorG4dget
我已经尽我所能将您的代码添加到您的OP。请确认它是正确的 – inspectorG4dget