2016-08-18 11 views
0

xml.etree.ElementTreeをPython 3.4から使用して、むしろlarge XML file(1つの聖書帳で)を解析しようとしています(Windowsとの互換性のために、ライブラリモジュール)、関連するメソッドがここにあります。いくつかのelement.tail属性は空ではありませんが、

class BibleTree: 
    def __init__(self, file_name: str) -> None: 
     self.root = ET.parse(file_name).getroot() 

    @staticmethod 
    def _list_to_clean_text(str_in: str) -> str: 
     out = re.sub(r'[\s\n]+', ' ', str_in, flags=re.DOTALL) 
     return out.strip() 

    @staticmethod 
    def _clean_text(intext: Optional[str]) -> str: 
     return intext if intext is not None else '' 

    def __iter__(self) -> Tuple[int, int, str]: 
     collected = None 
     cur_chap = 0 
     cur_verse = 0 

     for child in self.root: 
      if child.tag in ['kap', 'vers']: 
       if collected and collected.strip(): 
        yield cur_chap, cur_verse, self._list_to_clean_text(collected) 
       if child.tag == 'kap': 
        cur_chap = int(child.attrib['n']) 
       elif child.tag == 'vers': 
        cur_verse = int(child.attrib['n']) 
       collected = self._clean_text(child.tail) 
      else: 
       if collected is not None: 
        collected += self._clean_text(child.text) 
        collected += self._clean_text(child.tail) 

問題は、私見テキストであるべきであるが、いくつかの状況では(例えば、ライン54上の素子<odkazo/>)可変childtail属性は、いずれもないことです。

私は間違っていますか?

+0

コードではどうすればよいですか?私はそれを実行することはできませんので、コードは完全ではありません。例えば、定義されていない 'Optional'への参照があります。あなたが明確な[mcve]を提供できるなら、それは役に立ちます。 – mzjn

答えて

0

これはPEBKACです...私は、他の要素の中にマイルストーンされた要素がないと仮定しました。ですから、関数全体を再帰的なものとして書き直す必要があります。しかたがない。

関連する問題