2012-09-24 124 views
1

我是新手程序员,试图制作解析xml并将其内容粘贴到频道上的irc bot。通常我会在google上找到我的答案,但这次我找不到我的答案。Python:TypeError:不能乘以类型'float'的非int的序列

q0tag = dom.getElementsByTagName('hit')[0].toxml() 
q0 = q0tag.replace('<hit>','').replace('</hit>','') 

q1 = (q0 * 1.2) 

当我试图乘Q0它总是显示

TypeError: can't multiply sequence by non-int of type 'float'. 

林试图使Q0整数或浮点数,但它只是让另一个错误

AttributeError: 'NoneType' object has no attribute 'replace' 

Q0值是没有小数的整数。

回答

11

您的q0值仍然是一个字符串。这基本上是你在做什么:

>>> q0 = '3' 
>>> q1 = (q0 * 1.2) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
TypeError: can't multiply sequence by non-int of type 'float' 

为了解决这个问题,将字符串转换为数字第一:

>>> q1 = (float(q0) * 1.2) 
>>> q1 
3.5999999999999996 

您可能还需要寻找到了lxmlBeautifulSoup模块解析XML 。

+0

+1用于提及XML解析器。 [ElementTree](http://docs.python.org/library/xml.etree.elementtree.html)和[minidom](http://docs.python.org/library/xml.dom.minidom.html)分别是也相当不错。 – vinnydiehl

+0

我一开始做了,但删除了它。我意识到OP正在使用''xml.dom'',这可能是好的 – jterrace

+0

是的,我刚才在评论后看到了这一点。无论如何,我会保持链接,因为有人可能会发现它们有用。 – vinnydiehl

相关问题