【发布时间】:2020-07-14 14:55:32
【问题描述】:
我的 XML 结构如下:
<result>
<report>
<id>111</id>
<user>username1</user>
<actions_list>
<action1>
<id>a_1</id>
</action1>
<action1>
<id>a_2</id>
</action1>
<action1>
<id>a_3</id>
</action1>
</actions_list>
</report>
<report>
<id>222</id>
<user>username2</user>
<actions_list>
<action1>
<id>a_1</id>
</action1>
<action2>
<id>a_2</id>
</action2>
<action3>
<id>a_3</id>
</action3>
<action4>
<id>a_4</id>
</action4>
<action5>
<id>a_5</id>
</action5>
<actions_list>
</report>
</result>
所以,我想创建一个结构如下的 CSV 文件:
+---+-----+-----------+-----+
| 1 | 111 | username1 | a_1 |
+---+-----+-----------+-----+
| 1 | 111 | username1 | a_2 |
+---+-----+-----------+-----+
| 1 | 111 | username1 | a_3 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_1 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_2 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_3 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_4 |
+---+-----+-----------+-----+
| 2 | 222 | username2 | a_5 |
+---+-----+-----------+-----+
我尝试使用 python BeautifulSoup 和 xml.etree,但无法处理具有相同名称(在我的示例中为“id”)和不同报告中不同数量的操作的字段。我该怎么做?任何帮助将非常感激。提前致谢。
【问题讨论】: