python-3.x TypeError:列表索引必须是整数或切片,而不是字符串解析xml时出现错误

zte4gxcn  于 2022-11-19  发布在  Python
关注(0)|答案(1)|浏览(179)

我试着为我的设备写一个解析器。但是我不明白我犯了什么错误,因为我得到了错误:

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'])

有人能帮我吗?

b1zrtrql

b1zrtrql1#

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():
        if isinstance(value2, list):
            for item in value2:
                print(item['@id'])
                print(item['@type'])
                print(item['#text'])

相关问题