-2
   -<randomints count = 5>
       <ints>-2,-2,-2,-2,-2</ints>
       <ints>-2,-2,-1,-2,-2</ints>
       <ints>-2,-2,-2,-1,-2</ints>
    </randomints>

我用了xml.etree.ElementTree

Ints = []
for child in root.findall('randomints'):
    Ints = [l.text for l in child]

我得到的输出为['-2,-2,-2,-2,-2','-2,-2,-1,-2,-2','-2,-2,-2,-1,-2']

但我需要输出为[[-2,-2,-2,-2,-2],[-2,-2,-1,-2,-2],[-2,-2,-2,-1,-2]]

4

1 回答 1

1

尝试这个:

import xml.etree.ElementTree as ET

root = ET.parse("infile.xml")

for child in root.findall('randomints'):
    ints = [list(map(int, l.text.split(","))) for l in child]

print(ints)

输出:

[[-2, -2, -2, -2, -2], [-2, -2, -1, -2, -2], [-2, -2, -2, -1, -2]]
于 2020-02-18T15:13:47.567 回答