python使用minidom检索标记的整数值
我想恢复标记的整数值python使用minidom检索标记的整数值,python,xml,tags,minidom,Python,Xml,Tags,Minidom,我想恢复标记的整数值 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'
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"
但是如果我想转换这个字符串
convert = int(finalvalue)
我犯了以下错误
"ValueError invalid literal for int() with base 10: '5.22 ' "
使用拆分方法时,我得到以下结果:
[u'\n5.22',u'\n']在转换为整数之前,使用strip删除字符串中的空白,并使用float\u str将其转换为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
非常感谢兄弟,你是我的救世主^^