Parse and count numeric only xml text including e-00 or e+01

前端 未结 1 1485
太阳男子
太阳男子 2020-12-22 06:46

I am a python newbie. I am trying to parse through an xml file and count all text inputs that are all numeric including approximated values using e- or e+. E.g. Given the ps

相关标签:
1条回答
  • 2020-12-22 07:34

    You can simply try to convert each inner text element to a float, and ignore any errors.

    import xml.etree.ElementTree as ET
    
    tree = ET.parse("temp.txt")
    root = tree.getroot()
    nums = []
    
    for e in root.itertext():
        try:
            nums.append(float(e))
        except ValueError:
            pass
    
    print nums
    print len(nums)
    

    As requested, a probably inefficient but successful method to keep track of the locations of the elements:

    def extractNumbers(path, node):
        nums = []
    
        path += '/' + node.tag
        if 'name' in node.keys():
            path += '=' + node.attrib['name']
    
        try:
            num = float(node.text)
            nums.append( (path, num) )
        except (ValueError, TypeError):
            pass
    
        for e in list(node):
            nums.extend( extractNumbers(path, e) )
    
        return nums
    
    tree = ET.parse('temp.txt')
    nums = extractNumbers('', tree.getroot())
    print len(nums)
    print nums
    
    for n in nums:
        print n[0], n[1]
    
    0 讨论(0)
提交回复
热议问题