С xpath:
from lxml import etree
from collections import defaultdict
from pprint import pprint
doc="""<?xml version="1.0" encoding="utf-8"?>
<Basic>
<Segment>
<Sample value="12" data2="25" data3="23"/>
<Sample value="13" data2="0" data3="323"/>
<Sample value="14" data2="2" data3="3"/>
</Segment>
</Basic>
"""
el = etree.fromstring(doc)
data2 = el.xpath('//@data2')
dataX = el.xpath('//@*[starts-with(name(), "data")]')
print data2
print dataX
# With iteration over Sample elements, like in J.F. Sebastian answer, but with XPath
d = defaultdict(list)
for sample in el.xpath('//Sample'):
for attr_name, attr_value in sample.items():
d[attr_name].append(attr_value)
pprint(dict(d))
Вывод:
['25', '0', '2']
['25', '23', '0', '323', '2', '3']
{'data2': ['25', '0', '2'],
'data3': ['23', '323', '3'],
'value': ['12', '13', '14']}