我正在尝试将in的值添加到列表中。我需要使用xpath获取具有此值new_values = ['a','b']的列表
Sub Copy()
Dim c As Range
Dim j As Integer
Dim Source As Worksheet
Dim Target As Worksheet
Set Source = ActiveWorkbook.Worksheets("Input")
Set Target = ActiveWorkbook.Worksheets("Output")
j = 1
For Each c In Source.Range("C1:C4000")
If IsNumeric(Left(c.Value, 1)) Then
Source.Rows(c.Row).Copy Target.Rows(j)
j = j + 1
End If
Next c
xml.xml文件
import xml.etree.ElementTree as ET
parse = ET.parse('xml.xml')
[ record.find('events').text for record in parse.findall('.configuration/system/') ]
我的python代码的输出是仅包含一个值的列表-['a'],但我需要一个包含a和b的列表。
答案 0 :(得分:2)
您非常接近。您只需要使用findall('events')
并对其进行迭代即可获取所有值。
例如:
import xml.etree.ElementTree as ET
parse = ET.parse('xml.xml')
print([ events.text for record in parse.findall('.configuration/system/') for events in record.findall('events')])
输出:
['a', 'b']
答案 1 :(得分:2)
针对单个.findall()
调用进行了优化:
import xml.etree.ElementTree as ET
root = ET.parse('input.xml').getroot()
events = [e.text for e in root.findall('configuration/system//events')]
print(events)
configuration/system//events
-到events
元素的相对xpath 输出:
['a', 'b']