当前位置:   article > 正文

python解析xml的第三方库_Python利用第三方库lxml处理xml文件

python使用第三方库对xml进行解析

lxml处理xml文件代码:

from lxml import etree

# xmlPath = r'000020.xml' # 内容相对少

xmlPath = r'000005.xml' # 内容相对多

html = etree.parse(xmlPath, etree.HTMLParser())

# result = etree.tostring(html)

# print(result.decode('utf-8'))

# print(type(result)) #

# print(result)

# objectsList = html.xpath('//annotation/object')

# print(objectsList)

namesList = html.xpath('/html/body/annotation/object/name/text()')

# print(namesList)

difficultsList = html.xpath('//annotation/object/difficult/text()')

# print(difficultsList)

xminsList = html.xpath('//annotation/object/bndbox/xmin/text()')

# print(xminsList)

yminsList = html.xpath('//annotation/object/bndbox/ymin/text()')

# print(yminsList)

xmaxsList = html.xpath('//annotation/object/bndbox/xmax/text()')

# print(xmaxsList)

ymaxsList = html.xpath('//annotation/object/bndbox/ymax/text()')

# print(ymaxsList)

for i in range(len(difficultsList)):

difficult = difficultsList[i]

classType = namesList[i]

xmin = int(xminsList[i])

ymin = int(yminsList[i])

xmax = int(xmaxsList[i])

ymax = int(ymaxsList[i])

info = \

"difficult:{0:^4}classType:{1:^10}xmin:{2:^6}ymin:{3:^6}xmax:{4:^6}ymax:{5:^6}".format(

difficult,classType,xmin,ymin,xmax,ymax)

print(info)

print("--"*10)

objectsList = html.xpath('//object')

obj = objectsList[0]

# print(obj) #

xmin = obj.xpath('bndbox/xmin/text()')

print(xmin)

print("*"*20)

data = obj.xpath('//bndbox')

print(data)

控制台输出结果:

Windows PowerShell

版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\test> & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.358366026\pythonFiles\lib\python\debugpy\launcher' '55379' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\test\lxml库

处理xml文件 .py'

difficult: 0 classType: chair xmin: 263 ymin: 211 xmax: 324 ymax: 339

difficult: 0 classType: chair xmin: 165 ymin: 264 xmax: 253 ymax: 372

difficult: 1 classType: chair xmin: 5 ymin: 244 xmax: 67 ymax: 374

difficult: 0 classType: chair xmin: 241 ymin: 194 xmax: 295 ymax: 299

difficult: 1 classType: chair xmin: 277 ymin: 186 xmax: 312 ymax: 220

--------------------

['263']

********************

[, , , , ]

PS C:\Users\chenxuqi\Desktop\新建文件夹\test>

本文分享 CSDN - 敲代码的小风。

如有侵权,请联系 support@oschina.cn 删除。

本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/521323
推荐阅读
相关标签
  

闽ICP备14008679号