我试图定义一个类,该类将从包含多个字典的列表中生成一个标记列表(见下文),但当我尝试时,我得到以下回溯。我不确定我做错了什么。任何建议都将不胜感激
File "file.py", line 415, in <module>
p = Photo(data)
File "file.py", line 395, in __init__
for d in p_d["photo"]["tags"]["tag"]["_content"]:
TypeError: list indices must be integers or slices, not str
当前代码:
class Photo :
def __init__(self,p_d) :
self.tags = []
for d in p_d["photo"]["tags"]["tag"]["_content"]:
self.tags.append(d)
return
p = Photo(data)
print(p)
“数据”的内容看起来像this post中的“照片用语”。以下是带有标签的零件示例:
u'media':u'photo',
u'tags':{
u'tag':[
{
u'machine_tag':False,
u'_content':u'aerialview',
u'author':u'59600577@N07',
u'raw':u'Aerial View',
u'authorname':u'Patrick Foto ;)',
u'id':u'59579247-33334692904-8319'
},
{
u'machine_tag':False,
u'_content':u'buildingexterior',
u'author':u'59600577@N07',
u'raw':u'Building Exterior',
u'authorname':u'Patrick Foto ;)',
u'id':u'59579247-33334692904-1727027'
},
{
u'machine_tag':False,
u'_content':u'businessfinanceandindustry',
u'author':u'59600577@N07',
u'raw':u'Business Finance and Industry',
u'authorname':u'Patrick Foto ;)',
u'id':u'59579247-33334692904-263370815'
},
显然,
p_d["photo"]["tags"]["tag"]
是一个列表,您不能在列表中获取项目['_content']
你能行
相关问题 更多 >
编程相关推荐