用Python脚本读取xml里的数据
xml里的数据是
<?xml version='1.0' encoding='UTF-8'?>
<document filename="cTDaR_t00014.jpg">
<table>
<Coords points="2489,656 4213,656 4213,3242 2489,3242"/>
</table>
</document>
需要得到四个点的坐标,即p1=(2489,656),p2=(4213,656)……
我网上找的代码需要变成 首尾都有才行,求解答
该回答引用chatgpt:
import xml.etree.ElementTree as ET
# 解析XML文件
tree = ET.parse('/Users/changzhenwei/Desktop/text.xml')
root = tree.getroot()
# 找到Coords元素
coords_elem = root.find('.//Coords')
# 获取points属性值
points = coords_elem.attrib['points']
# 解析points属性值
points_list = points.split(' ')
coords = [tuple(map(int, point.split(','))) for point in points_list]
# 打印坐标
for i, coord in enumerate(coords, start=1):
print(f'p{i} = {coord}')
我们将在下面的例子中使用这个 XML 文档。