Loading [MathJax]/jax/input/TeX/config.js
社区首页 >问答首页 >用xml.etree.ElementTree解析某些元素的问题

用xml.etree.ElementTree解析某些元素的问题
EN

Stack Overflow用户
提问于 2021-09-12 09:48:36
回答 2查看 97关注 0票数 0

我希望你一切都好。我面临着与解析器相关的一些困难。实际上,我的数据集如下所示:

代码语言:javascript
代码运行次数:0
复制
<?xml version="1.0"?>

<bugrepository name="AspectJ">
  <bug id="28974" opendate="2003-1-3 10:28:00" fixdate="2003-1-14 14:30:00">
    <buginformation>
      <summary>"Compiler error when introducing a ""final"" field"</summary>
      <description>The aspecs the problem...</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/AjcMemberMaker.java</file>
    </fixedFiles>
  </bug>

  <bug id="28919" opendate="2002-12-30 16:40:00" fixdate="2003-1-14 15:06:00">
    <buginformation>
      <summary>waever tries to weave into native methods ...</summary>
      <description>If youat org.aspectj.ajdt.internal.core.burce</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/LazyMethodGen.java</file>
    </fixedFiles>
  </bug>
  
  <bug id="29186" opendate="2003-1-8 21:22:00" fixdate="2003-1-14 16:43:00">
    <buginformation>
      <summary>ajc -emacssym chokes on pointcut that includes an intertype method</summary>
      <description>This ;void Foo.ajc$before$Foo</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/Lint.java</file>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/Shadow.java</file>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java</file>
    </fixedFiles>
  </bug>
  
  <bug id="29769" opendate="2003-1-19 11:42:00" fixdate="2003-1-24 21:17:00">
    <buginformation>
      <summary>Ajde does not support new AspectJ 1.1 compiler options</summary>
      <description>The org.aspectj.ajpiler. This enhancement is needed byort.</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/ajde/testdata/examples/figures-coverage/figures/Figure.java</file>
      <file>org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/AjdeTests.java</file>
      <file>org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/ui/StructureViewManagerTest.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/ajc/BuildArgParser.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildConfig.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/testsrc/org/aspectj/ajdt/ajc/BuildArgParserTestCase.java</file>
    </fixedFiles>
  </bug>
  <bug id="29959" opendate="2003-1-22 7:10:00" fixdate="2003-2-13 16:00:00">
    <buginformation>
      <summary>super call in intertype method declaration body causes VerifyError</summary>
      <description>AspectJ Compiler 1.1 showstopper</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/compiler/ast/InterTypeConstructorDeclaration.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/ast/SuperFixerVisitor.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMethodBinding.java</file>
      <file>org.aspectj/modules/tests/bugs/SuperToIntro.java</file>
    </fixedFiles>
  </bug>
</bugrepository>

我希望能够恢复数据集的某些元素,以便在dataframe中与Pandas一起使用它们。

第一个问题是以列表形式从标记中获取所有子元素。

实际上,我的代码只检索第一个元素,忽略其他元素,或者可以检索所有元素,但不像在这些图片中看到的那样结构化:这里只有空([])列表没有内容

守则:

代码语言:javascript
代码运行次数:0
复制
import pandas as pd 
from xml.etree.ElementTree import parse

document = parse('dataset.xml')
summary = []
description = []
fixedfile = []

for item in document.iterfind('bug'):
    summary.append(item.findtext('buginformation/summary'))
    description.append(item.findtext('buginformation/description'))
    fixedfile.append(item.findall('fixedFiles/file'))
    
#df = pd.DataFrame({'summary':summary, 'description':description, 'fixed_files':fixedfile})
df = pd.DataFrame({'fixed_files': fixedfile})
df

这里只有第一个元素

守则:

代码语言:javascript
代码运行次数:0
复制
import pandas as pd 
from xml.etree.ElementTree import parse

document = parse('dataset.xml')
summary = []
description = []
fixedfile = []

for item in document.iterfind('bug'):
    summary.append(item.findtext('buginformation/summary'))
    description.append(item.findtext('buginformation/description'))
    fixedfile.append(item.findtext('fixedFiles/file'))
    
#df = pd.DataFrame({'summary':summary, 'description':description, 'fixed_files':fixedfile})
df = pd.DataFrame({'fixed_files': fixedfile})
df

我在这里发现了一个适合我的情况的“使用Python遍历xml.etree.ElementTree树的问题”解决方案,它可以工作,但不像我想要的那样(每个元素的列表),我可以加载所有的元素,但是可以单独加载。

守则:

代码语言:javascript
代码运行次数:0
复制
import xml.etree.ElementTree as ET
import pandas as pd 

xmldoc = ET.parse('dataset.xml')
root = xmldoc.getroot()
summary = []
description = []
fixedfile = []

for bug in xmldoc.iter(tag='bug'): 
    
    #for item in document.iterfind('bug'):
    #summary.append(item.findtext('buginformation/summary'))
    #description.append(item.findtext('buginformation/description'))
    
    for file in bug.iterfind('./fixedFiles/file'):
    
           fixedfile.append([file.text])
        
fixedfile
#df = pd.DataFrame({'summary':summary, 'description':description, 'fixed_files':fixedfile})
df = pd.DataFrame({'fixed_files': fixedfile})
df

当我想迭代我的数据的其他列(摘要,描述)时,我得到以下错误消息: ValueError:所有数组必须具有相同的长度

第二个问题,例如能够选择所有有2或3个子元素的标记。

诚挚的问候,

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-09-12 10:04:45

若要将文件保存在与描述和摘要关联的列表中,请将它们添加到每个错误的新列表中。

Try:

代码语言:javascript
代码运行次数:0
复制
import pandas as pd
from xml.etree.ElementTree import parse

document = parse('dataset.xml')
summary = []
description = []
fixedfile = []

for item in document.iterfind('bug'):
    summary.append(item.findtext('buginformation/summary'))
    description.append(item.findtext('buginformation/description'))
    fixedfile.append([elt.text for elt in item.findall('fixedFiles/file')])

df = pd.DataFrame({'summary': summary,
                   'description': description,
                   'fixed_files': fixedfile})
df

对于第二部分,这将只过滤那些有两个或更多文件的bug。

代码语言:javascript
代码运行次数:0
复制
newdf = df[df.fixed_files.str.len() >= 2]

如果想要有2和3个文件的bug,那么:

代码语言:javascript
代码运行次数:0
复制
newdf = df[(df.fixed_files.str.len() == 2) | (df.fixed_files.str.len() == 3)]
票数 1
EN

Stack Overflow用户

发布于 2021-09-12 10:10:02

下面收集数据。这样做的目的是找到所有的bug元素并对它们进行迭代。对于每个bug -查找所需的子元素。

代码语言:javascript
代码运行次数:0
复制
import xml.etree.ElementTree as ET
import pandas as pd

xml = '''<?xml version="1.0"?>

<bugrepository name="AspectJ">
  <bug id="28974" opendate="2003-1-3 10:28:00" fixdate="2003-1-14 14:30:00">
    <buginformation>
      <summary>"Compiler error when introducing a ""final"" field"</summary>
      <description>The aspecs the problem...</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/AjcMemberMaker.java</file>
    </fixedFiles>
  </bug>

  <bug id="28919" opendate="2002-12-30 16:40:00" fixdate="2003-1-14 15:06:00">
    <buginformation>
      <summary>waever tries to weave into native methods ...</summary>
      <description>If youat org.aspectj.ajdt.internal.core.burce</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/LazyMethodGen.java</file>
    </fixedFiles>
  </bug>
  
  <bug id="29186" opendate="2003-1-8 21:22:00" fixdate="2003-1-14 16:43:00">
    <buginformation>
      <summary>ajc -emacssym chokes on pointcut that includes an intertype method</summary>
      <description>This ;void Foo.ajc$before$Foo</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/Lint.java</file>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/Shadow.java</file>
      <file>org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java</file>
    </fixedFiles>
  </bug>
  
  <bug id="29769" opendate="2003-1-19 11:42:00" fixdate="2003-1-24 21:17:00">
    <buginformation>
      <summary>Ajde does not support new AspectJ 1.1 compiler options</summary>
      <description>The org.aspectj.ajpiler. This enhancement is needed byort.</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/ajde/testdata/examples/figures-coverage/figures/Figure.java</file>
      <file>org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/AjdeTests.java</file>
      <file>org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/ui/StructureViewManagerTest.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/ajc/BuildArgParser.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildConfig.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/testsrc/org/aspectj/ajdt/ajc/BuildArgParserTestCase.java</file>
    </fixedFiles>
  </bug>
  <bug id="29959" opendate="2003-1-22 7:10:00" fixdate="2003-2-13 16:00:00">
    <buginformation>
      <summary>super call in intertype method declaration body causes VerifyError</summary>
      <description>AspectJ Compiler 1.1 showstopper</description>
    </buginformation>
    <fixedFiles>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/compiler/ast/InterTypeConstructorDeclaration.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/ast/SuperFixerVisitor.java</file>
      <file>org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMethodBinding.java</file>
      <file>org.aspectj/modules/tests/bugs/SuperToIntro.java</file>
    </fixedFiles>
  </bug>
  </bugrepository>'''

data = []
root = ET.fromstring(xml)
for bug in root.findall('.//bug'):
    bug_info = bug.find('buginformation')
    fixed_files = bug.find('fixedFiles')
    entry = {'summary': bug_info.find('summary').text,'description':bug_info.find('summary').text,'fixedFiles':[x.text for x in list(fixed_files)]}
    data.append(entry)
for entry in data:
    print(entry)
df = pd.DataFrame(data)

输出

代码语言:javascript
代码运行次数:0
复制
{'summary': '"Compiler error when introducing a ""final"" field"', 'description': '"Compiler error when introducing a ""final"" field"', 'fixedFiles': ['org.aspectj/modules/weaver/src/org/aspectj/weaver/AjcMemberMaker.java']}
{'summary': 'waever tries to weave into native methods ...', 'description': 'waever tries to weave into native methods ...', 'fixedFiles': ['org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/LazyMethodGen.java']}
{'summary': 'ajc -emacssym chokes on pointcut that includes an intertype method', 'description': 'ajc -emacssym chokes on pointcut that includes an intertype method', 'fixedFiles': ['org.aspectj/modules/weaver/src/org/aspectj/weaver/Lint.java', 'org.aspectj/modules/weaver/src/org/aspectj/weaver/Shadow.java', 'org.aspectj/modules/weaver/src/org/aspectj/weaver/bcel/BcelWeaver.java']}
{'summary': 'Ajde does not support new AspectJ 1.1 compiler options', 'description': 'Ajde does not support new AspectJ 1.1 compiler options', 'fixedFiles': ['org.aspectj/modules/ajde/testdata/examples/figures-coverage/figures/Figure.java', 'org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/AjdeTests.java', 'org.aspectj/modules/ajde/testsrc/org/aspectj/ajde/ui/StructureViewManagerTest.java', 'org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/ajc/BuildArgParser.java', 'org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/core/builder/AjBuildConfig.java', 'org.aspectj/modules/org.aspectj.ajdt.core/testsrc/org/aspectj/ajdt/ajc/BuildArgParserTestCase.java']}
{'summary': 'super call in intertype method declaration body causes VerifyError', 'description': 'super call in intertype method declaration body causes VerifyError', 'fixedFiles': ['org.aspectj/modules/org.aspectj.ajdt.core/src/org/compiler/ast/InterTypeConstructorDeclaration.java', 'org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/ast/SuperFixerVisitor.java', 'org.aspectj/modules/org.aspectj.ajdt.core/src/org/aspectj/ajdt/internal/compiler/lookup/InterTypeMethodBinding.java', 'org.aspectj/modules/tests/bugs/SuperToIntro.java']}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69153935

复制
相关文章
炼丹志 | 2021显卡挑选指南
来源丨https://zhuanlan.zhihu.com/p/365926059
公众号机器学习与AI生成创作
2021/07/05
1.8K0
NVIDIA Pascal Geforce显卡揭秘
新一代NVIDIA“帕斯卡”(Pascal)架构显卡即将发布,各种传言也如火如荼,最近网络上流传一张图揭露全新的Geforce X家族,包含了基于GP104的X80、基于GP100的X80 ti和X8
GPUS Lady
2018/03/30
1.4K0
NVIDIA Pascal Geforce显卡揭秘
创意影响:为什么色彩准确的视频编辑监视器很重要
把你的工作展示在世界面前可能会很可怕。但是,当你对自己的内容感觉良好时——当你相信它时——焦虑就会变成兴奋。恐惧变成了信心。和消息连接。当准确及时地分享强有力的想法比以往任何时候都更能定义我们时,这一点尤其紧迫。在BenQ,我们相信如果您的媒体是视频,并且您的方法涉及编辑,那么使用正确的视频编辑显示器至关重要。
IT胶囊
2021/10/19
3820
创意影响:为什么色彩准确的视频编辑监视器很重要
NVIDIA双风扇GeForce RTX散热器破坏多GPU性能,是真的么?
今日Lady发现国外一篇文章提到测试多片NVIDIA双风扇Geforce RTX时,会出现性能降低的状况 (点击阅读原文访问原始文章)。
GPUS Lady
2018/10/23
1.5K0
NVIDIA双风扇GeForce RTX散热器破坏多GPU性能,是真的么?
NVIDIA GeForce Experience 登录失败解决
2、找到NVIDIA FrameView SDK service,这个服务如果处于关闭状态就打开,如果处于开启状态就重启
kenvie
2022/01/20
5.7K0
NVIDIA GeForce Experience 登录失败解决
5.2 vim颜色显示和移动光标
vim颜色显示 不同的文件,或者说相同的文件在不同的目录下,就会有可能导致颜色不存在 在centos系统中,/etc下的文件,往往都是都是配置文件,在/etc下一般都会显示颜色,放置在其他位置则不会显示颜色 在/etc目录下的文件,有颜色显示 复制过来的文件,在/tmp目录下,无颜色显示 在一般模式下(即是刚打开的文件) 按两次 gg ,光标会定位到首行,首字母 按大写的字母 O (或者是快捷键shift+o),光标就会定位到刚刚所在的位置的上一行去,并进入编辑模式 然后输入# 和
运维小白
2022/01/06
2.2K0
5.2 vim颜色显示和移动光标
ubuntu16.04安装cuda9.0(ubuntu18安装nvidia驱动)
Ubuntu 下安装CUDA需要装NVIDIA驱动,首先进入NVIDIA官网,然后查询对应NVIDIA驱动是否支持你电脑的型号。
全栈程序员站长
2022/07/29
7380
ubuntu16.04安装cuda9.0(ubuntu18安装nvidia驱动)
电脑硬件知识入门之显卡篇
上一篇文章 电脑硬件知识入门之CPU篇 我们介绍了cpu的一些基本知识,那么这篇文章我们继续谈一下另一个电脑的核心部件,显卡。
再见孙悟空_
2023/02/10
7850
关于英伟达显卡命名的姿势
平时在实验中用到GPU的地方比较多,看新闻也总是能看到英伟达又出了什么型号的显卡等等,可是我一直没搞清楚该公司显卡名称的命名关系,今天特地查了下,总结在这里,以便以后翻阅。 Nvidia的GPU命名有4个层次:
王云峰
2019/12/25
2.2K0
AMD FreeSync正式发布:更流畅 更便宜
2013年10月份,NVIDIA推出了一项革命性的垂直同步技术G-Sync,通过新控制模块的加入配合GeForce GPU控制显示器刷新率,彻底解决垂直同步、刷新率限制带来的游戏画面撕裂、卡顿、延迟等问题,保证最好的流畅度。
全栈程序员站长
2022/09/07
1.3K0
学电脑必知的电脑配置
电脑的配置,主要看CPU、显卡、主板、内存、硬盘、显示器等,而笔记本的话就看它的品牌就行了。国外的有HP、apple、松下、东芝等,不过顾客口碑和质量比较硬的是DELL和HP这两个品牌;国产的有:宏基、清华紫光、清华同方、神州、海尔、联想、八亿时空等。
全栈程序员站长
2022/09/13
2K0
tensorflow各个版本的CUDA以及Cudnn版本对应关系
(1)NVIDIA的显卡驱动程序和CUDA完全是两个不同的概念哦!CUDA是NVIDIA推出的用于自家GPU的并行计算框架,也就是说CUDA只能在NVIDIA的GPU上运行,而且只有当要解决的计算问题是可以大量并行计算的时候才能发挥CUDA的作用。
全栈程序员站长
2022/06/27
5.9K0
tensorflow各个版本的CUDA以及Cudnn版本对应关系
w7设置双显示器_4K+144Hz 支持FreeSync Acer XV273K显示器评测
2018年末,AMD宣布旗下FreeSync技术正式升级为Radeon FreeSync 2 HDR技术,带来了亮度、对比度、层次感更加完美的游戏画面,尤其是针对HDR游戏。而在随后的CES 2019上,NVIDIA对G-Sync进行了重新分级,其中G-Sync Compatible标准正式开启FreeSync显示器兼容模式。两大“劲敌”的一系列动作意味着,未来FreeSync显示器或将成为更多游戏玩家的首选电竞显示器。
全栈程序员站长
2022/08/23
1.2K0
w7设置双显示器_4K+144Hz 支持FreeSync Acer XV273K显示器评测
RealVNC Server Ubuntu 20.04 无显示器连接 虚拟显示器
以前尝试过完全不接显示器,vnc连接设置总是不成功,这次很容易做成功了,记录一下。
全栈程序员站长
2022/09/24
2.6K0
RealVNC Server Ubuntu 20.04 无显示器连接 虚拟显示器
显卡相关技术名词解析2
渲染管线也称为渲染流水线,是显示芯片内部处理图形信号相互独立的的并行处理单元渲染管线的数量一般是以 像素渲染流水线的数量×每管线的纹理单元数量 来表示渲染管线的数量是决定显示芯片性能和档次的最重要的参数之一,在相同的显卡核心频率下,更多的渲染管线也就意味着更大的像素填充率和纹理填充率。
reizhi
2022/09/26
3630
显示器不亮?解决“显示器不支持当前的输入时序,请将输入时序更改为 1920x1080, 60Hz”的终极指南
在服务器重启后,接上显示器却发现显示器黑屏,且提示“输入时序不支持”,要求将分辨率和刷新率设置为 1920x1080,60Hz。这个问题常见于 Windows 服务器,但在 Linux 服务器上也会偶尔遇到类似情况。为解决这个问题,我们将分别介绍在 Windows 和 Linux 系统下的解决方案。
猫头虎
2024/11/02
1.1K0
显示器不亮?解决“显示器不支持当前的输入时序,请将输入时序更改为 1920x1080, 60Hz”的终极指南
windows虚拟显示器SDK开发和提供
这周末闲来无事,整理了下虚拟显示器的源码,发现有几个项目都用到了,但是使用的功能不尽相同: (1)最简单的运用仅仅是需要显示器的拔插; (2)稍微复杂一点的是需要设置显示器的分辨率,包括标准的分辨率,如19201080,还包括非标准的分辨率,如1120900; (3)再复杂一点的需要设置显示器名称、刷新频率、获取虚拟显示器屏幕图像信息。 再观察我的那几个项目,都重复的导入了源码,每次发现一个bug,改了一个,另外一个忘记同步了,造成代码维护的不便,所以此次将虚拟显示器部分的代码进行提取,单独封装成一个SDK,只要项目中有用到都使用这个SDK即可。
全栈程序员站长
2022/09/24
8100
windows虚拟显示器SDK开发和提供
存在即合理!“伪需求”的VR背包电脑其实当前市场广阔
随着E3 2017的结束,那些出现在E3上的游戏新品也将陆续登陆玩家们的电脑。而对于目前内容吃紧的VR行业来说,本届E3出现了诸多VR大作,在一定程度上缓解了内容匮乏的窘状。不知你是否与小编一样,在V
VRPinea
2018/05/16
9310
【玩转 GPU】Windows系统下tensorflow-gpu2.10看图急速入门
1.1Windows下anaconda安装(针对win10、win11 64位版本)
用户8320413
2023/08/15
9350
【玩转 GPU】Windows系统下tensorflow-gpu2.10看图急速入门
如何配置一台深度学习工作站?
这篇文章主要介绍的是家用的深度学习工作站,典型的配置有两种,分别是一个 GPU 的机器和四个 GPU的机器。如果需要更多的 GPU 可以考虑配置两台四个 GPU 的机器。
AI算法与图像处理
2019/08/23
3.4K0
如何配置一台深度学习工作站?

相似问题

将div id传递给PHP变量

10

将div id存储在php变量中?

46

在php变量中捕获div id值

24

如何将div id值传递给PHP变量?

22

中的div幻灯片将PHP id变量传递给jquery。

12
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文