2014-06-05 74 views
0

標籤的整數值,我想恢復標籤蟒蛇檢索與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'\n']" 

finalvalue = ''.join(str1) 

    "return 5.22" 

的整數值,但如果我想這串字符

convert = int(finalvalue) 

我有以下錯誤轉換

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

當我使用split方法我得到以下結果:

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

回答

0

使用strip()轉換成整數,&使用浮動(the_str)將其轉換爲浮動之前去除從字符串空格。

>>> 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 
+0

非常感謝bro.you是我的救命恩人^^ – user3710113