TypeError: 列表索引必须是整数或切片,解析 xml 时不会出现 str 错误
我尝试为我的设备编写解析。但我无法理解我在哪里犯了错误,因为我收到了错误:
print(value2['@id'])
TypeError: list indices must be integers or slices, not str
您可能会在下面看到我的代码:
import requests
import xmltodict
url = 'http://192.168.1.8:8060/query/apps'
text = requests.get(url).text
#content = """
#<?xml version="1.0" encoding="UTF-8"?>
#<apps>
#<app id="31012" type="menu" #version="2.0.53">Vudu Movie & #TV Store</app>
#</apps>
#"""
text = text.split('\n')
text = text[1:]
text = ''.join(text)
data = xmltodict.parse(text)
data = dict(data)
for key1, value1 in data.items():
for key2, value2 in value1.items():
print(value2['@id'])
print(value2['@type'])
print(value2['#text'])
有人可以帮我吗?
I tried to write a parses for my device. But I can't undestood where I made mistake, because I got the error:
print(value2['@id'])
TypeError: list indices must be integers or slices, not str
My code you may see below:
import requests
import xmltodict
url = 'http://192.168.1.8:8060/query/apps'
text = requests.get(url).text
#content = """
#<?xml version="1.0" encoding="UTF-8"?>
#<apps>
#<app id="31012" type="menu" #version="2.0.53">Vudu Movie & #TV Store</app>
#</apps>
#"""
text = text.split('\n')
text = text[1:]
text = ''.join(text)
data = xmltodict.parse(text)
data = dict(data)
for key1, value1 in data.items():
for key2, value2 in value1.items():
print(value2['@id'])
print(value2['@type'])
print(value2['#text'])
Could anyone help me with it, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)