TypeError: 列表索引必须是整数或切片,解析 xml 时不会出现 str 错误

发布于 2025-01-18 06:52:33 字数 772 浏览 0 评论 0原文

我尝试为我的设备编写解析。但我无法理解我在哪里犯了错误,因为我收到了错误:

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 &amp; #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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以酷 2025-01-25 06:52:33
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():
        if isinstance(value2, list):
            for item in value2:
                print(item['@id'])
                print(item['@type'])
                print(item['#text'])
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():
        if isinstance(value2, list):
            for item in value2:
                print(item['@id'])
                print(item['@type'])
                print(item['#text'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文