python - 将某些行从一个 XML 文件传输到另一个
问题描述
我正在处理来自 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="<p>I have an absolutely positioned <code>div</code> containing several children, one of which is a relatively positioned <code>div</code>. When I use a <strong>percentage-based width</strong> on the child <code>div</code>, it collapses to '0' width on <a href="http://en.wikipedia.org/wiki/Internet_Explorer_7" rel="noreferrer">Internet&nbsp;Explorer&nbsp;7</a>, but not on Firefox or Safari.</p>

<p>If I use <strong>pixel width</strong>, it works. If the parent is relatively positioned, the percentage width on the child works.</p>

<ol>
<li>Is there something I'm missing here?</li>
<li>Is there an easy fix for this besides the <em>pixel-based width</em> on the
child?</li>
<li>Is there an area of the CSS specification that covers this?</li>
</ol>
" 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="<pytorch><hick><css3><internet-explorer-7>" 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="<p>I have an absolutely positioned <code>div</code> containing several children, one of which is a relatively positioned <code>div</code>. When I use a <strong>percentage-based width</strong> on the child <code>div</code>, it collapses to '0' width on <a href="http://en.wikipedia.org/wiki/Internet_Explorer_7" rel="noreferrer">Internet&nbsp;Explorer&nbsp;7</a>, but not on Firefox or Safari.</p>

<p>If I use <strong>pixel width</strong>, it works. If the parent is relatively positioned, the percentage width on the child works.</p>

<ol>
<li>Is there something I'm missing here?</li>
<li>Is there an easy fix for this besides the <em>pixel-based width</em> on the
child?</li>
<li>Is there an area of the CSS specification that covers this?</li>
</ol>
" 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="<pytorch><css><css3><internet-explorer-7>" AnswerCount="6" CommentCount="0" FavoriteCount="12" />
</posts>
解决方案
Thank you for all the help in the comments!!
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)
Alternativ for
'Tags'
matching:
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')
推荐阅读
- optimization - AutoML 手动选择模型
- laravel-8 - Laravel 8 工匠服务
- python - 如何在 django rest 框架中的嵌套 serailizers 上执行发布请求
- java - Spring boot:JSON将带有时区的日期和时间反序列化为LocalDateTime
- join - 如何为 Flink 中的 MapState 中的所有项目设置 TTL?
- c - 为什么解除引用的指针递增/递减不起作用?
- django - 使用 get 和 request 将 id 用户检查到两个字段中
- python - Caddy reverse_proxy 不适用于 Python Flask Server
- xpath - 如何替换topmenu(网站)的联系我们按钮的链接
- java - 非法转换:无法从“java.lang.String”转换为“java.sql.Blob”