-3

私は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)

しかし、それは機能しません

4

3 に答える 3

0

これがうまくいくかどうかを確認してください:

#!/usr/bin/env python
#-*- coding:utf-8 -*-
from xml.dom import minidom
xmldoc  = minidom.parse('/path/to/file')
items   = xmldoc.getElementsByTagName('Datei') 

for s in items:
    if s.attributes['Titel'].value == "Morgen 1":
        with open("Morgen 1.txt", "w") as fileOutput:
            listLines = [   line.strip()
                            for line in s.firstChild.nodeValue.strip().split("\n")
                            if line.strip()
                            ]

            fileOutput.write("\n".join(listLines))
            break
于 2012-12-22T09:55:48.510 に答える
-1

xml を使用せずにこれを行うための簡単な方法 (推奨) が必要な場合は、次のようにします。

with open('path/to/input') as infile:
    found = False
    outfile = open("Morgen 1.txt", 'w')
    for line in infile:
        if line.startswith("<Datei") and 'Titel="Morgen 1"' in line:
            found = True
        elif line.startswith("</Datei"):
            found = False
        if found:
            if not line.startswith("<Datei"):
                outfile.write(line)
于 2012-12-22T09:23:30.847 に答える
-2

これを試してみてください...うまくいきます...

fp = open("data.txt", "r")
data = fp.read();

data = data.split(">");

i = 0;

while True:
    filename = data[i].split('" ')[1].split('"')[1]
    text = data[i+1].split('<')[0].strip()

    fp1 = open(filename + ".txt", "w")
    fp1.write(text)
    fp1.close()

    i += 2
    if i >= (len(data) - 1):
        break;
于 2012-12-22T09:16:40.153 に答える