0

Python の etree.ElementTree パッケージを使用して xml ファイルを解析しましたが、失敗しているようです。

私のxmlファイル階層は次のようになっています: root <- -> config data <> sourcefile <- -> file object1 object2 ...など.

print self.xml_root.findall(".\config") を使用すると、空のリストである "[]" しか取得できませんでした。

4

1 に答える 1

2

本当に'.\config'文字列を持っている場合、それが問題になります。\cこれは、文字の1 つとして使用する文字列リテラルです。'.\\config'またはr'.\config'があり、どちらもリテラルのバックスラッシュを指定している場合でも、それはまだ間違っています。

$ cat eleme.py
import xml.etree.ElementTree as ET

root = ET.fromstring("""
<root>
  <config>
    source
  </config>
  <config>
    source
  </config>
</root>""")

print r'using .\config', root.findall('.\config')
print r'using .\\config', root.findall('.\\config')
print 'using ./config', root.findall('./config')
$ python2.7 eleme.py 
using .\config []
using .\\config []
using ./config [<Element 'config' at 0x8017a8610>, <Element 'config' at 0x8017a8650>]
于 2013-07-14T21:59:19.750 に答える