Nguyen Cuong:
Je XML comme format suivant
<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>
Maintenant, je voudrais obtenir la valeur comme une paire de x et y de l'étiquette, tels que (403,79), (399,79) ...
Je l'ai essayé, mais seulement obtenir les valeurs de type chaîne
root = tree.getroot()
for item in root.findall('mark'):
svg = item.findall('svg')
svg_value = t[0].text
Pourrais-je en mesure d'obtenir comme type de valeur dictionnaire?
Balderman:
Au dessous de
(La chaîne JSON à l' intérieur svg était invalide et devait être fixé)
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)
production
[[{'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'}]]