2017-08-26 100 views
0

我想從一系列xml數據中提取數字。用Python從xml中提取數據3

xml數據是這樣的:

<commentinfo> 
    <note>This file contains the sample data for testing</note> 
    <comments> 
    <comment> 
     <name>Romina</name> 
     <count>97</count> 
</comment> 

等都與新的名稱和註釋。

我的代碼是:

import urllib.request, urllib.parse, urllib.error 
import xml.etree.ElementTree as ET 

url = 'http://py4e-data.dr-chuck.net/comments_42.xml' 

uh = urllib.request.urlopen(url) 
data = uh.read() 
# print(data) 

tree = ET.fromstring(data) 
# print('Name:',tree.find('count').text) 
lst = tree.findall('comments/comment/count') 
# print(len(lst)) 
# print(lst) 
# x1 = result[1].find('comment') 

# for item in lst: 
#  print('Count', item.find('count').text) 

counts = tree.findall('.//count') 
print(counts) 

當我打印counts我得到一個更長的版本:

<Element 'count' at 0x000000000A09FB88>, <Element 'count' at 0x000000000A09FC78>, <Element 'count' at 0x000000000A09FD68>, <Element 'count' at 0x000000000A09FE58>, <Element 'count' at 0x000000000A09FF48>, <Element 'count' at 0x000000000A0A3098>] 

我很新的這一點,所以我不明白爲什麼我收到這些十六進制數字,我也不知道如何提取實際數字。

我希望有人可以幫忙。

+0

'counts.text'是否滿足您的要求? –

回答

1

只是遍歷列表並打印每個元素的文本。

import urllib.request, urllib.parse, urllib.error 
import xml.etree.ElementTree as ET 

url = 'http://py4e-data.dr-chuck.net/comments_42.xml' 

uh = urllib.request.urlopen(url) 
data = uh.read() 

tree = ET.fromstring(data) 

lst = tree.findall('comments/comment/count') 

counts = tree.findall('.//count') 

for each in counts: 
    print(each.text) 
+0

太棒了!謝謝。 –