Python Issues with a Class

谁说我不能喝 提交于 2019-12-25 05:00:30

问题


I am having issues with my below class. I keep getting the below traceback, butI am not sure were I am going wrong. I am expecting to see a dictionary with photo tags. Any help would be great.

Traceback:

File "project.py", line 231, in <module>
    new_photo = Photo(result_get_photo_data)
  File "project.py", line 228, in __init__
    for diction in photo_diction["photo"]["tags"]["tag"]:
TypeError: list indices must be integers, not st

My Code:

class Photo(object): 
    def __init__(self,photo_diction):
        self.tags = []
        for diction in photo_diction["photo"]["tags"]["tag"]:
            return self.tags.append(diction)

new_photo = Photo(result_get_photo_data)
print new_photo.tags

Some Contents of Photo Diction, its a list that contains several dictionaries.

[  
   {  
      u'photo':{  
         u'people':{  
            u'haspeople':0
         },
         u'dateuploaded':u'1492780609',
         u'owner':{  
            u'username':u'Patrick Foto ;)',
            u'realname':u'',
            u'nsid':u'59600577@N07',
            u'iconserver':u'2943',
            u'location':u'Bangkok,
            Thailand',
            u'path_alias':u'patrick_foto',
            u'iconfarm':3
         },
         u'publiceditability':{  
            u'canaddmeta':0,
            u'cancomment':1
         },
         u'id':u'33334692904',
         u'title':{  
            u'_content':u'Kuala Lumpur skyline'
         },
         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'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'capitalcities',
                  u'author':u'59600577@N07',
                  u'raw':u'Capital Cities',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-711565'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'citylife',
                  u'author':u'59600577@N07',
                  u'raw':u'City Life',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-7631'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'cloudsky',
                  u'author':u'59600577@N07',
                  u'raw':u'Cloud - Sky',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-834640'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'colorimage',
                  u'author':u'59600577@N07',
                  u'raw':u'Color Image',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-872992'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'downtowndistrict',
                  u'author':u'59600577@N07',
                  u'raw':u'Downtown District',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-5634066'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'futuristic',
                  u'author':u'59600577@N07',
                  u'raw':u'Futuristic',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-61798'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'nopeople',
                  u'author':u'59600577@N07',
                  u'raw':u'No People',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-9003'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'photography',
                  u'author':u'59600577@N07',
                  u'raw':u'Photography',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1935'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'traveldestinations',
                  u'author':u'59600577@N07',
                  u'raw':u'Travel Destinations',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-872991'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'urbanskyline',
                  u'author':u'59600577@N07',
                  u'raw':u'Urban Skyline',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-153762'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'architecture',
                  u'author':u'59600577@N07',
                  u'raw':u'architecture',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1174'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'business',
                  u'author':u'59600577@N07',
                  u'raw':u'business',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-11698'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'city',
                  u'author':u'59600577@N07',
                  u'raw':u'city',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-103'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'cityscape',
                  u'author':u'59600577@N07',
                  u'raw':u'cityscape',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-3643'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'day',
                  u'author':u'59600577@N07',
                  u'raw':u'day',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-3511'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'growth',
                  u'author':u'59600577@N07',
                  u'raw':u'growth',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-12192'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'horizontal',
                  u'author':u'59600577@N07',
                  u'raw':u'horizontal',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1049'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'kualalumpur',
                  u'author':u'59600577@N07',
                  u'raw':u'kuala lumpur',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-24092'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'malaysia',
                  u'author':u'59600577@N07',
                  u'raw':u'malaysia',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-15385'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'modern',
                  u'author':u'59600577@N07',
                  u'raw':u'modern',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1180'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'outdoors',
                  u'author':u'59600577@N07',
                  u'raw':u'outdoors',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1860'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'panoramic',
                  u'author':u'59600577@N07',
                  u'raw':u'panoramic',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-9696'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'sky',
                  u'author':u'59600577@N07',
                  u'raw':u'sky',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-282'
               },
               {  
                  u'machine_tag':False,
                  u'_content':u'skyscraper',
                  u'author':u'59600577@N07',
                  u'raw':u'skyscraper',
                  u'authorname':u'Patrick Foto ;)',
                  u'id':u'59579247-33334692904-1306'
               },

回答1:


I see two issues here.

  1. Your photo_dictation is a list. You need either:

    for dictation in photo_diction[0]["photo"]["tags"]["tag"]:
    

    or a double loop:

    for photo in photo_dictation:
       for dictation in photo["photo"]["tags"]["tag"]
    
  2. you are returning within your loop. I think you probably want:

    class Photo(object): 
        def __init__(self,photo_diction):
            self.tags = []
            for diction in photo_diction["photo"]["tags"]["tag"]:
                self.tags.append(diction)
            return
    


来源:https://stackoverflow.com/questions/43543917/python-issues-with-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!