XMLファイルで辞書の値を返す方法

グエンクオン:

私は、次のフォーマットとしてXMLを持っています

<case>
    <number>162</number>
    <age>40</age>
    <sex>F</sex>
    <composition>solid</composition>
    <echogenicity>hypoechogenicity</echogenicity>
    <margins>ill defined</margins>
    <calcifications>non</calcifications>
    <tirads>4c</tirads>
    <reportbacaf/>
    <reporteco/>
    <mark>
        <image>1</image>
        <svg>[{"points": [{"x": 403, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}, "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
</case>

今、私はそのような(403,79)、(399,79)と、タグからのxとyのペアとして値を取得したいと思います...

私が試しただけで、文字列型の値を取得しています

root = tree.getroot()
for item in root.findall('mark'):
svg = item.findall('svg')
svg_value = t[0].text

辞書値型としてそれを得るために私ができるだろうか?

balderman:

未満

(内部のJSON文字列SVGは無効だったと固定されなければなりませんでした)

import xml.etree.ElementTree as ET
import json


xml = '''<case>
    <number>162</number>
    <age>40</age>
    <sex>F</sex>
    <composition>solid</composition>
    <echogenicity>hypoechogenicity</echogenicity>
    <margins>ill defined</margins>
    <calcifications>non</calcifications>
    <tirads>4c</tirads>
    <reportbacaf/>
    <reporteco/>
    <mark>
        <image>1</image>
        <svg>[{"points": [{"x": 493, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}], "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
     <mark>
        <image>5</image>
        <svg>[{"points": [{"x": 343, "y": 79}, {"x": 399, "y": 79}, {"x": 391, "y": 78}, {"x": 379, "y": 82}, {"x": 373, "y": 88}, {"x": 368, "y": 99}], "annotation": {}, "regionType": "freehand"}]
        </svg>
    </mark>
</case>'''

root = ET.fromstring(xml)
svg_lst = [s.text for s in root.findall('.//svg')]
data = [json.loads(svg) for svg in svg_lst]
print(data)

出力

[[{'points': [{'x': 493, 'y': 79}, {'x': 399, 'y': 79}, {'x': 391, 'y': 78}, {'x': 379, 'y': 82}, {'x': 373, 'y': 88}, {'x': 368, 'y': 99}], 'annotation': {}, 'regionType': 'freehand'}], [{'points': [{'x': 343, 'y': 79}, {'x': 399, 'y': 79}, {'x': 391, 'y': 78}, {'x': 379, 'y': 82}, {'x': 373, 'y': 88}, {'x': 368, 'y': 99}], 'annotation': {}, 'regionType': 'freehand'}]]

おすすめ

転載: http://10.200.1.11:23101/article/api/json?id=384038&siteId=1