我有一个函数,该函数返回Reddit注释及其最多n级的答复,我面临如何提取注释并复制并将其保存到CSV的困难。功能如下,输出也可以写
def comments_to_dicts(comments):
results = []
for comment in comments:
item = {
"id": comment.id,
"author": comment.author,
"up votes": comment.ups,
"comment-text":comment.body,
}
if len(comment._replies) > 0:
item["replies"] = comments_to_dicts(comment._replies)
results.append(item)
return results
这是输出,我只显示一个。
[{'id': 'e5bpnup', 'author': Redditor(name='AnxiousSun'), 'up votes': 38, 'comment-text': 'Maps is getting way too bloated and uses way too much rich-media. The explore tab could be its own app. ', 'replies': [{'id': 'e5bu127', 'author': Redditor(name='baspeysp'), 'up votes': 9, 'comment-text': 'Way too bloated, I only use it to locate a place but rarely to do anything about the location, got better apps for that. ', 'replies': [{'id': 'e5c2txt', 'author': Redditor(name='YupSuprise'), 'up votes': 3, 'comment-text': "I haven't heard of any apps for that, mind dropping some recommendations? ", 'replies': [{'id': 'e5chm3o', 'author': Redditor(name='moralesnery'), 'up votes': 2, 'comment-text': "maybe he's talking about another GPS Navigation APP?\n\nIf that's the case we have HERE We Go, Sygic, Tom-Tom, Karta, MAPS.ME, OsmAnd, etc."}]}, {'id': 'e5d9oa2', 'author': Redditor(name='jojo_31'), 'up votes': 1, 'comment-text': 'Yeah me too, the maps themselves kind of suck, rather use OsmAnd'}]}]}]
答案 0 :(得分:0)
您应该将答复作为results
列表中单独的元素添加,而不是将答复添加到字典中的新关键字。这将使层次结构扁平化,因此您可以将其另存为CSV。
def comments_to_dicts(comments, parentid = 0):
results = []
for comment in comments:
item = {
"id": comment.id,
"author": comment.author,
"up votes": comment.ups,
"comment-text": comment.body,
"in-reply-to": parentid
}
results.append(item)
for reply in comment._replies:
results += comments_to_dicts(reply, comment.id)
return results
我在字典中添加了in-reply-to
字段,以便您可以从CSV重构层次结构。