0

I would like to recover the integer value of a tag

from xml.dom import minidom

docXML = minidom.parse('/root/Desktop/doc.xml')

node = docXML.getElementsByTagName('span')[0]

value = node.firstChild.data 

     " return value is 5.22%"

str1 = value.split('%') 

    "return [u'\n5.22', u'\n']"

finalvalue = ''.join(str1) 

     "return 5.22"

but if I would to convert this string character

convert = int(finalvalue)

I got the following error

"ValueError  invalid literal for int() with base 10: '5.22 ' "

When I use the split method I get the following result:

[u'\n5.22', u'\n']

BoJack Horseman
  • 4,406
  • 13
  • 38
  • 70

1 Answers1

0

use strip() to remove whitespaces from the string before converting to integer, & use float(the_str) to convert it to float.

>>> num_str = '5.22 '
>>> int(num_str)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '5.22 '
>>> float(num_str.strip())
5.22
DhruvPathak
  • 42,059
  • 16
  • 116
  • 175