Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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中提取XML数据:打印数据,但不返回总和_Python_Xml_Sum - Fatal编程技术网

在Python中提取XML数据:打印数据,但不返回总和

在Python中提取XML数据:打印数据,但不返回总和,python,xml,sum,Python,Xml,Sum,我的代码编译并返回输出中的数据。我要求打印数字的总和,但程序不打印。我的总结有什么问题吗?我应该分开打印报表吗 以下是我写的: import urllib import xml.etree.ElementTree as ET serviceurl = 'http://python-data.dr-chuck.net/comments_42.xml' while True: url = serviceurl + urllib.urlencode({'sensor':'false', 'a

我的代码编译并返回输出中的数据。我要求打印数字的总和,但程序不打印。我的总结有什么问题吗?我应该分开打印报表吗

以下是我写的:

import urllib
import xml.etree.ElementTree as ET

serviceurl = 'http://python-data.dr-chuck.net/comments_42.xml'
while True:
    url = serviceurl + urllib.urlencode({'sensor':'false', 'address': 'address'})
    print ('Retrieving', url)
    uh = urllib.urlopen(url)
    data = uh.read()
    print ('Retrieved',len(data),'characters')
    print (data)
    tree = ET.fromstring(data)
    results = tree.findall('.//count')
print (results, sum(results))
这显示了XML数据的外观:

<comment>
  <name>Matthias</name>
  <count>97</count>
</comment>

马蒂亚斯
97
尝试以下方法:

print(results, sum([float(x) for x in results]))

您必须将
string
转换为可求和的内容,如
int
float
您需要解析count标记中的数字,并将其转换为int对象,以便进行求和

如果有多个计数,您可以将它们拉入一个列表,然后遍历该列表以获得总和

例如:

totcomm = 0    # set initial sum value to zero
counts = tree.findall('comments/comment')  # create list of comments
for item in counts:                        # iterate through list 
   x = item.find('count').text             # pass count content to x
   totcomm = tot + int(x)                  # add integer of x to total
print totcomm
我还将重新构造程序,这样您就不会重复返回URL(虽然为True),而只需进行一次调用并将所有数据传递给“data”变量。在这种情况下,您只需要:

url = 'http://python-data.dr-chuck.net/comments_42.xml'
print 'Retrieving', url
诸如此类。

我真的很感谢你的建议,感谢你们所有人!这是我对眼前问题的建议。。。
不能对字符串求和(将数字传递给求和,而不是字符串)。顺便说一句,当你运行这个程序时,你不会说会发生什么,这对你的问题的读者来说并不像你所能做的那样有帮助——这会有很大的不同!什么版本?您在Python3中使用的是print as,在Python2中使用的是urllib.urlencode。我已经安装了这两个Python。当程序运行时,它编译并返回URL中的数据和文件中的字符数。没有打印任何其他内容。我应该有类似的内容:count=[“comment”][0][“count”]count=tree[“comment”][0][“count”]谢谢大家的输入。他们非常有助于我理解自己的错误。
import urllib
import xml.etree.ElementTree as ET

url = raw_input('Enter location: ')
if len(url) == 0:
   url = 'http://python-data.dr-chuck.net/comments_189090.xml'    
uh = urllib.urlopen(url)
data = uh.read()
tree = ET.fromstring(data)
count = 0
totalcounts = 0                            # set initial sum value to zero
counts = tree.findall('comments/comment')  # create list of comments
for item in counts:                        # iterate through list 
   x = item.find('count').text             # pass count content to x
   totalcounts = totalcounts + int(x)      # add integer of x to total
   count = count + 1
print count   
print totalcounts