Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python使用minidom检索标记的整数值_Python_Xml_Tags_Minidom - Fatal编程技术网

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

非常感谢兄弟,你是我的救世主^^