How to ignore or pass xpath xml IndexError in Python

bgrand

New Member
How can I force python to ignore \[code\]IndexError\[/code\] without using \[code\]try\[/code\] & \[code\]except\[/code\] every single value that I am extracting? My XML have multiple values that needed to be extracted. Some records don't have the value / at root[0], so I have to manually use \[code\]try\[/code\] & \[code\]except IndexError:\[/code\] for every single node that I am extracting.Here's my code:\[code\]try: a = etree.XPath('/Data/a/b/nodeA/text()')(root)[0] except IndexError: a = ''try: b = etree.XPath('/Data/a/b/x/y/nodeB/text()')(root)[0] except IndexError: b = ''try: c = etree.XPath('/Data/a/b/d/nodeB/text()')(root)[0] except IndexError: c = ''\[/code\]
 
Back
Top