将某些行从一个XML文件传输到另一个

时间:2019-03-05 01:51:50

标签: python xml elementtree

我正在处理stackoverflow转储中的所有帖子。因为它太大了,并且我的任何程序都需要花费很长时间才能运行,所以我想创建一个单独的XML文件,其中仅包含带有我感兴趣的标签的帖子。我正在尝试使用ElementTree来完成此任务任务。 我可以找到所需的帖子,但是将它们写到另一个XML文件时遇到了麻烦。

import xml.etree.ElementTree as ET

if __name__ == '__main__':
    posts = ET.Element('data')
    row = ER.SubElement(posts, "row")
    tree = ET.parse('Posts.xml')
    root = tree.getroot()

    for child in root:
        if child.get('Tags') and 'pytorch' in child.get('Tags') or child.get('Tags') and 'tensorflow' in child.get('Tags') or child.get('Tags') and 'keras' in child.get('Tags'):
            ET.SubElement(row, child)

    mydata = ET.tostring(posts)
    myfile = open("subposts.xml", "w")
    myfile.write(mydata)

但是,我得到了错误:

 File "/local/mez2113/stackoverflow/create_sub_posts.py", line 13, in <module>
    mydata = ET.tostring(posts)
  File "/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1136, in tostring
    short_empty_elements=short_empty_elements)
  File "/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 774, in write
    qnames, namespaces = _namespaces(self._root, default_namespace)
  File "/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 886, in _namespaces
    _raise_serialization_error(tag)
  File "/opt/anaconda3/lib/python3.7/xml/etree/ElementTree.py", line 1058, in _raise_serialization_error
    "cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize <Element 'row' at 0x7f2b2f9dcf98> (type Element)

原始XML示例:

<posts>
      <row Id="6" PostTypeId="1" AcceptedAnswerId="31" CreationDate="2008-07-31T22:08:08.620" Score="261" ViewCount="16799" Body="&lt;p&gt;I have an absolutely positioned &lt;code&gt;div&lt;/code&gt; containing several children, one of which is a relatively positioned &lt;code&gt;div&lt;/code&gt;. When I use a &lt;strong&gt;percentage-based width&lt;/strong&gt; on the child &lt;code&gt;div&lt;/code&gt;, it collapses to '0' width on &lt;a href=&quot;http://en.wikipedia.org/wiki/Internet_Explorer_7&quot; rel=&quot;noreferrer&quot;&gt;Internet&amp;nbsp;Explorer&amp;nbsp;7&lt;/a&gt;, but not on Firefox or Safari.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;If I use &lt;strong&gt;pixel width&lt;/strong&gt;, it works. If the parent is relatively positioned, the percentage width on the child works.&lt;/p&gt;&#xA;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Is there something I'm missing here?&lt;/li&gt;&#xA;&lt;li&gt;Is there an easy fix for this besides the &lt;em&gt;pixel-based width&lt;/em&gt; on the&#xA;child?&lt;/li&gt;&#xA;&lt;li&gt;Is there an area of the CSS specification that covers this?&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;" OwnerUserId="9" LastEditorUserId="63550" LastEditorDisplayName="Rich B" LastEditDate="2016-03-19T06:05:48.487" LastActivityDate="2018-10-16T16:54:34.953" Title="Percentage width child element in absolutely positioned parent on Internet Explorer 7" Tags="&lt;pytorch&gt;&lt;hick&gt;&lt;css3&gt;&lt;internet-explorer-7&gt;" AnswerCount="6" CommentCount="0" FavoriteCount="12" />
      <row Id="6" PostTypeId="1" AcceptedAnswerId="31" CreationDate="2008-07-31T22:08:08.620" Score="261" ViewCount="16799" Body="&lt;p&gt;I have an absolutely positioned &lt;code&gt;div&lt;/code&gt; containing several children, one of which is a relatively positioned &lt;code&gt;div&lt;/code&gt;. When I use a &lt;strong&gt;percentage-based width&lt;/strong&gt; on the child &lt;code&gt;div&lt;/code&gt;, it collapses to '0' width on &lt;a href=&quot;http://en.wikipedia.org/wiki/Internet_Explorer_7&quot; rel=&quot;noreferrer&quot;&gt;Internet&amp;nbsp;Explorer&amp;nbsp;7&lt;/a&gt;, but not on Firefox or Safari.&lt;/p&gt;&#xA;&#xA;&lt;p&gt;If I use &lt;strong&gt;pixel width&lt;/strong&gt;, it works. If the parent is relatively positioned, the percentage width on the child works.&lt;/p&gt;&#xA;&#xA;&lt;ol&gt;&#xA;&lt;li&gt;Is there something I'm missing here?&lt;/li&gt;&#xA;&lt;li&gt;Is there an easy fix for this besides the &lt;em&gt;pixel-based width&lt;/em&gt; on the&#xA;child?&lt;/li&gt;&#xA;&lt;li&gt;Is there an area of the CSS specification that covers this?&lt;/li&gt;&#xA;&lt;/ol&gt;&#xA;" OwnerUserId="9" LastEditorUserId="63550" LastEditorDisplayName="Rich B" LastEditDate="2016-03-19T06:05:48.487" LastActivityDate="2018-10-16T16:54:34.953" Title="Percentage width child element in absolutely positioned parent on Internet Explorer 7" Tags="&lt;pytorch&gt;&lt;css&gt;&lt;css3&gt;&lt;internet-explorer-7&gt;" AnswerCount="6" CommentCount="0" FavoriteCount="12" />
</posts>

1 个答案:

答案 0 :(得分:0)

感谢您在评论中的所有帮助!

import xml.etree.ElementTree as ET

if __name__ == '__main__':
    posts = ET.Element('data')
    tree = ET.parse('Sub_posts.xml')
    root = tree.getroot()

    for child in root:
        if child.get('Tags') and 'pytorch' in child.get('Tags') or child.get('Tags') and 'tensorflow' in child.get('Tags') or child.get('Tags') and 'keras' in child.get('Tags'):
            posts.append(child)

    mydata = ET.tostring(posts).decode()
    myfile = open("subposts.xml", "w")
    myfile.write(mydata)
  

替代进行'Tags'匹配:

tags1 = set(['pytorch', 'tensorflow', 'keras'])
for child in root:
    if tags1 & set([t[1:] for t in child.get('Tags').split('>') if t]):
        print('match')