【问题标题】:python prints document{} when try to iterate over xml and print tags and attributespython 在尝试迭代 xml 并打印标签和属性时打印文档{}
【发布时间】:2020-01-13 04:17:07
【问题描述】:

我有一个如下所示的 xml 文档:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="CoreNLP-to-HTML.xsl" type="text/xsl"?>
<root>
  <document>
    <docId>CVE-2002-2131.txt</docId>
    <sentences>
      <sentence id="1">
        <tokens>
          <token id="1">
            <word>Directory</word>
            <lemma>Directory</lemma>
            <CharacterOffsetBegin>0</CharacterOffsetBegin>
            <CharacterOffsetEnd>9</CharacterOffsetEnd>
            <POS>NNP</POS>
          </token>
          <token id="2">
            <word>traversal</word>
            <lemma>traversal</lemma>
            <CharacterOffsetBegin>10</CharacterOffsetBegin>
            <CharacterOffsetEnd>19</CharacterOffsetEnd>
            <POS>NN</POS>
          </token>
          <token id="3">
            <word>vulnerability</word>
            <lemma>vulnerability</lemma>
            <CharacterOffsetBegin>20</CharacterOffsetBegin>
            <CharacterOffsetEnd>33</CharacterOffsetEnd>
            <POS>NN</POS>
          </token>
          <token id="4">
            <word>in</word>
            <lemma>in</lemma>
            <CharacterOffsetBegin>34</CharacterOffsetBegin>
            <CharacterOffsetEnd>36</CharacterOffsetEnd>
            <POS>IN</POS>
          </token>
           ...

由于某种原因,当我尝试遍历 xml 并打印子标签和属性时,它正在打印文档 {}。

这是我的代码:

print("-------")
#extract from xml file and get fields ready for csv for this document
newFilePathOneXml=os.path.join("ners",filenameWithoutExtension,'CVE-2002-2131.txt.xml')
print(newFilePathOneXml)
from xml.etree import ElementTree as et
tree = et.parse(newFilePathOneXml)
print("here2")
root = tree.getroot()
print("here3")
for child in root:
    print("here4")
    print (child.tag, child.attrib)
    print("here5")
#f = open(newFilePathOneXml,"r")
#print(f.read())
et.dump(myRoot)

这是打印出来的:

---
ners\nvdcve-1.1-2002\CVE-2002-2131.txt.xml
here2
here3
here4
document {}
here5
...........prints xml document here................

我正在尝试按照指向get the xml out and access with child tags 的链接中的说明进行操作。我真正需要的是每个标记元素中称为 word 的标签。我将获取单词值并写入字符串。

知道为什么它不起作用吗?文件名为 cve-2002-2131.txt.xml。

更新:我添加了文件读取和打印文件内容,然后尝试了 et.dump(myRoot),它确实以这种方式打印了整个 xml,就像读取文件和打印 (f.read()) 一样。

更新2: 我得到了这个工作,但我不确定获取令牌元素值的最佳方法是什么。

for elem in tree.iter():
    print (elem)

打印:

here3
<Element 'root' at 0x000002588AF2A5E8>
here4
here5
<Element 'document' at 0x000002588AF2A548>
here4
here5
<Element 'docId' at 0x000002588AF2AB88>
here4
here5
<Element 'sentences' at 0x000002588AF89638>
here4
here5
<Element 'sentence' at 0x000002588AF89688>
here4
here5
<Element 'tokens' at 0x000002588AF89CC8>
here4
here5
<Element 'token' at 0x0000025884CFAC28>
...

我在看iterate over tree,有一个关于 tree.findall 的答案,但它在做奇怪的事情。

tokens=tree.findall('.//tokens')
print(tokens)
#for token in range(len(tokens))  #this gives error
#    print(token)

打印:

[<Element 'tokens' at 0x00000222B2059CC8>, <Element 'tokens' at 0x00000222ABDF7958>, <Element 'tokens' at 0x00000222ABDFE318>]

【问题讨论】:

    标签: python xml file


    【解决方案1】:

    我找到了解决方案:

    print("here3")
    tokens=tree.findall('.//tokens')
    #print(tokens)
    for tokensItem in tokens: #range(len(tokens))
        for token in tokensItem:
            print(token.find('word').text)
    

    不过可能有更有效的方法,欢迎分享。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 2019-12-17
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2012-08-20
      相关资源
      最近更新 更多