Python 使用.append将列表追加到词典时出错

Python 使用.append将列表追加到词典时出错,python,list,dictionary,praw,Python,List,Dictionary,Praw,我正在努力抓取reddit,我已经有了一本字典(reddit_sneaker_info),上面有“帖子标题”、“帖子id”、“创建的时间/日期”和“评论数”,每只鞋都是这样: 'Puma Clyde Hardwood': [{'Post_title': 'Puma Clyde Hardwood Natural, originally got these to ball in but these definitely looks good for casual use too.', 'Pos

我正在努力抓取reddit,我已经有了一本字典(reddit_sneaker_info),上面有“帖子标题”、“帖子id”、“创建的时间/日期”和“评论数”,每只鞋都是这样:

'Puma Clyde Hardwood': [{'Post_title': 'Puma Clyde Hardwood Natural, originally got these to ball in but these definitely looks good for casual use too.',
   'Post_id': 'ki4kfi',
   'Created_at': 1608669448.0,
   'Num_comments': 0}
因此,对于每一次跑步,我希望它能够循环浏览每一只鞋的帖子,并将每一篇帖子的所有评论添加到一个列表中(comments\u list)。我想把每个帖子的评论数量限制在30条。然后,一旦它将所有注释附加到comments\u列表中,获取comments\u列表并将其作为名为“comments”的新键添加到现有词典中。所以看起来是这样的:

'Puma Clyde Hardwood': [{'Post_title': 'Puma Clyde Hardwood Natural, originally got these to ball in but these definitely looks good for casual use too.',
   'Post_id': 'ki4kfi',
   'Created_at': 1608669448.0,
   'Num_comments': 0,
   'Comments':[(this is where the comments_list would go)]}
我尝试了一些在堆栈溢出上发现的方法,但没有成功。下面的代码是我最后一次尝试的代码,这是我遇到的常见错误:

---> 33         reddit_sneaker_info[shoe][post].append(comments_list)

TypeError: list indices must be integers or slices, not dict
我不知道还能做什么,我真的很感激任何帮助或纠正。谢谢大家!

#loop through each sneaker in the entire set
for shoe in reddit_sneaker_info:
    # for each sneaker extract the id number of the post
    for post in reddit_sneaker_info[shoe]:
        number_of_comments = post["Num_comments"]
        post_id = post["Post_id"]
        # empty list for comments to be re-added in 
        comments_list = []
        if number_of_comments != 0:
            # get the comments by post_id
            submission = reddit.submission(id=post_id)
            submission.comments.replace_more(limit=None)
            for comment in submission.comments.list():
                comment_count += 1
                # prevent the script from grabbing more than 30 comments
                if comment_count < 30:
                    comment_by_post = comment.body
                    # append each comment text to list
                    comments_list.append(comment_by_post)
                    
                elif comment_count == 30:
                    break
        elif number_of_comments == 0:
            pass
        # append the comment list to the dictionary for that post
        reddit_sneaker_info[shoe][post].append(comments_list)
        post_count += 1
        # hopefully this is enough time in between requests
        time.sleep(45)
    print("Comments grabbed for shoe number {}".format(post_count))

我决定用逗号创建一个新的数据框架,而不是将它添加到现有的字典中。也许有更好的办法,但我想这是一个解决办法。在数据框中,我还将添加鞋名和帖子id,它们与评论的来源相对应

comments_list = []
post_id_list = []
shoe_name_list = []

for shoe in reddit_sneaker_info:
    # for each sneaker extract the id number of the post
    for post in reddit_sneaker_info[shoe]:
        number_of_comments = post["Num_comments"]
        post_id = post["Post_id"]
        comment_count = 0
        if number_of_comments != 0:
            # get the comments by post_id
            submission = reddit.submission(id=post_id)
            submission.comments.replace_more(limit=None)
            for comment in submission.comments.list():
                comment_count += 1
                # prevent the script from grabbing more than 30 comments
                if comment_count < 30:
                    comment_by_post = comment.body
                    shoe_name_list.append(shoe)
                    post_id_list.append(post_id)
                    # append each comment text to list
                    comments_list.append(comment_by_post)
                    print('comment added')
                    
                elif comment_count == 30:
                    print('limit of 30 comments reached')
                    break
        elif number_of_comments == 0:
            pass
        post_count += 1
        print("please wait 45 seconds")
        # hopefully this is enough time in between requests
        time.sleep(45)
    print("Comments grabbed for post number {}".format(post_count))
comments\u list=[]
post_id_list=[]
鞋子名称列表=[]
对于reddit_运动鞋中的鞋子信息:
#对于每一双运动鞋,提取帖子的id号
在reddit_sneaker_信息[鞋]中发布:
评论数量=帖子[“评论数量”]
post\u id=post[“post\u id”]
注释计数=0
如果评论的数量!=0:
#通过post_id获取评论
提交=reddit.submission(id=post_id)
提交。评论。替换更多(限制=无)
在submission.comments.list()中查找注释:
注释计数+=1
#防止脚本获取超过30条注释
如果注释计数小于30:
comment\u by\u post=comment.body
鞋\名称\列表。附加(鞋)
post\u id\u list.append(post\u id)
#将每个注释文本追加到列表中
评论列表。附加(评论由帖子发布)
打印('添加注释')
elif注释计数==30:
打印('已达到30条评论的限制')
打破
elif注释的数量=0:
通过
post_计数+=1
打印(“请等待45秒”)
#希望在请求之间有足够的时间
时间。睡眠(45)
打印(“为帖子编号{}捕获的评论”。格式(帖子计数))

;这与附加无关。你应该分享一点redddit\u sneaker\u信息。它很可能有一个目录。@BuddyBob我用更多的reddit_sneaker_信息编辑了原始帖子。你说得对,这是一个字典列表。那么这是否意味着我必须再往前走一层,或者我该怎么做呢?
comments_list = []
post_id_list = []
shoe_name_list = []

for shoe in reddit_sneaker_info:
    # for each sneaker extract the id number of the post
    for post in reddit_sneaker_info[shoe]:
        number_of_comments = post["Num_comments"]
        post_id = post["Post_id"]
        comment_count = 0
        if number_of_comments != 0:
            # get the comments by post_id
            submission = reddit.submission(id=post_id)
            submission.comments.replace_more(limit=None)
            for comment in submission.comments.list():
                comment_count += 1
                # prevent the script from grabbing more than 30 comments
                if comment_count < 30:
                    comment_by_post = comment.body
                    shoe_name_list.append(shoe)
                    post_id_list.append(post_id)
                    # append each comment text to list
                    comments_list.append(comment_by_post)
                    print('comment added')
                    
                elif comment_count == 30:
                    print('limit of 30 comments reached')
                    break
        elif number_of_comments == 0:
            pass
        post_count += 1
        print("please wait 45 seconds")
        # hopefully this is enough time in between requests
        time.sleep(45)
    print("Comments grabbed for post number {}".format(post_count))