XML 列表元素将字符串转换为整数
XML list element convert string to int
我从网上读入了一个XML文件,解码后放入树中进行解析:
data = openedurl.read()
xml = data.decode()
tree = ET.fromstring(xml)
counts = tree.findall('.//count')
我取回了以下计数元素:
<Element 'commentinfo' at 0x7fe9821c0040> <class
'xml.etree.ElementTree.Element'>
我检查了类型及其列表:
列表中填充的是字符串形式的数字。目标是将其从字符串转换为整数。
我知道我能做到:
mylist =[]
for count in counts:
mylist.append(int(count.text))
我也能做到:
mylist = [int(count.text) for count in counts]
我尝试使用 map
但我没有成功,我得到:
counts = list(map(int, counts))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'xml.etree.ElementTree.Element'
有没有办法使用 map
来做到这一点?
我还应该没有为理解创建一个新列表 mylist
而只是做:
counts = [int(count.text) for count in counts]
我只是想改进代码并更好地理解是什么以及为什么。
谢谢
在前两种情况下,您执行 count.text
以获得 text
。但是,当使用 map
时,您只需将 int
函数直接应用到 count
,而不是 count.text
.
由于 count
是一个 Element
对象而不是字符串,您会得到错误:
TypeError: int() argument must be a string, a bytes-like object or a
number, not 'xml.etree.ElementTree.Element'
如果您想使用 map
获取计数列表,您可以在 map
中使用 lambda expression,如下所示:
mylist = list(map(lambda x: int(x.text), counts))
这样,int
函数将应用于 count
中的字符串(在本例中为 x.text
)。
关于你的第二个问题
counts = [int(count.text) for count in counts]
对比
mylist = [int(count.text) for count in counts]
这完全是您的选择。如果您想稍后重新使用原始 counts
,请不要覆盖它,而是创建一个新的 mylist
;否则,完全可以覆盖现有的 counts
.
一段 PFB 代码,它将找到所有计数元素的总和:
import urllib.request
import xml.etree.ElementTree as ET
url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
data = response.read()
xml = data.decode()
tree = ET.fromstring(xml)
counts = tree.findall('.//count')
count_lst = []
sum = 0
for count in counts:
count_lst.append(int(count.text))
sum = sum + int(count.text)
print('count_lst: ', count_lst)
print('sum of all counts: ', sum)
输出:
count_lst: [97, 97, 90, 90, 88, 87, 87, 80, 79, 79, 78, 76, 76, 72, 72, 66, 66, 65, 65, 64, 61, 61, 59, 58, 57, 57, 54, 51, 49, 47, 40, 38, 37, 36, 36, 32, 25, 24, 22, 21, 19, 18, 18, 14, 12, 12, 9, 7, 3, 2]
sum of all counts: 2553
我从网上读入了一个XML文件,解码后放入树中进行解析:
data = openedurl.read()
xml = data.decode()
tree = ET.fromstring(xml)
counts = tree.findall('.//count')
我取回了以下计数元素:
<Element 'commentinfo' at 0x7fe9821c0040> <class 'xml.etree.ElementTree.Element'>
我检查了类型及其列表:
列表中填充的是字符串形式的数字。目标是将其从字符串转换为整数。
我知道我能做到:
mylist =[]
for count in counts:
mylist.append(int(count.text))
我也能做到:
mylist = [int(count.text) for count in counts]
我尝试使用 map
但我没有成功,我得到:
counts = list(map(int, counts))
TypeError: int() argument must be a string, a bytes-like object or a number, not 'xml.etree.ElementTree.Element'
有没有办法使用 map
来做到这一点?
我还应该没有为理解创建一个新列表 mylist
而只是做:
counts = [int(count.text) for count in counts]
我只是想改进代码并更好地理解是什么以及为什么。
谢谢
在前两种情况下,您执行 count.text
以获得 text
。但是,当使用 map
时,您只需将 int
函数直接应用到 count
,而不是 count.text
.
由于 count
是一个 Element
对象而不是字符串,您会得到错误:
TypeError: int() argument must be a string, a bytes-like object or a number, not 'xml.etree.ElementTree.Element'
如果您想使用 map
获取计数列表,您可以在 map
中使用 lambda expression,如下所示:
mylist = list(map(lambda x: int(x.text), counts))
这样,int
函数将应用于 count
中的字符串(在本例中为 x.text
)。
关于你的第二个问题
counts = [int(count.text) for count in counts]
对比
mylist = [int(count.text) for count in counts]
这完全是您的选择。如果您想稍后重新使用原始 counts
,请不要覆盖它,而是创建一个新的 mylist
;否则,完全可以覆盖现有的 counts
.
一段 PFB 代码,它将找到所有计数元素的总和:
import urllib.request
import xml.etree.ElementTree as ET
url = 'http://py4e-data.dr-chuck.net/comments_42.xml'
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
data = response.read()
xml = data.decode()
tree = ET.fromstring(xml)
counts = tree.findall('.//count')
count_lst = []
sum = 0
for count in counts:
count_lst.append(int(count.text))
sum = sum + int(count.text)
print('count_lst: ', count_lst)
print('sum of all counts: ', sum)
输出:
count_lst: [97, 97, 90, 90, 88, 87, 87, 80, 79, 79, 78, 76, 76, 72, 72, 66, 66, 65, 65, 64, 61, 61, 59, 58, 57, 57, 54, 51, 49, 47, 40, 38, 37, 36, 36, 32, 25, 24, 22, 21, 19, 18, 18, 14, 12, 12, 9, 7, 3, 2]
sum of all counts: 2553