Python Praw方法存储数据以便以后调用?

时间:2017-09-05 21:44:50

标签: python praw

字典是正确的方法吗?理想情况下,这将超过5+深。对不起,我唯一的语言体验是powershell,我只会制作一个对象数组。我不是在找人写代码我只是想知道是否有更好的方法?

由于 科迪

我的Powershell方式:

[$title1,$title2,$title3]

$titleX.comment = "comment here"
$titleX.comment.author = "bob"
$titleX.comment.author.karma = "200"
$titleX.comment.reply = "Hey Bob love your comment." 
$titleX.comment.reply.author = "Alex"
$titleX.comment.reply.reply = "I disagree"

Python代码Borken:

import praw


d = {}



reddit = praw.Reddit(client_id='XXXX',
                     client_secret='XXXX',
                     user_agent='android:com.example.myredditapp:'
                     'v1.2.3 (by /u/XXX)')

for submission in reddit.subreddit('redditdev').hot(limit=2):
     d[submission.id] = {}
     d[submission.id]['comment'] = {}
     d[submission.id]['title']= {}
     d[submission.id]['comment']['author']={}
     d[submission.id]['title'] = submission.title
     mySubmission = reddit.submission(id=submission.id)
     mySubmission.comments.replace_more(limit=0)
     for comment in mySubmission.comments.list():
        d[submission.id]['comment'] = comment.body
        d[submission.id]['comment']['author'] = comment.author.name

print(submission.title)
print(comment.body)
print(comment.author.name)
print(d)

  File "C:/git/tensorflow/Reddit/pull.py", line 23, in <module>
    d[submission.id]['comment']['author'] = comment.author.name

TypeError: 'str' object does not support item assignment

{'6xg24v': {'comment': 'Locking this version.  Please comment on the [original post](https://www.reddit.com/r/changelog/comments/6xfyfg/an_update_on_the_state_of_the_redditreddit_and/)!', 'title': 'An update on the state of the reddit/reddit and reddit/reddit-mobile repositories'}}

1 个答案:

答案 0 :(得分:1)

我认为您使用字典的方法是可以的,但您也可以通过为帖子使用数据结构来解决这个问题:而不是写

d[submission.id] = {}
d[submission.id]['comment'] = {}
d[submission.id]['title']= {}
d[submission.id]['comment']['author']={}
d[submission.id]['title'] = submission.title

您可以像这样创建一个类Submission

class Submission(object):
    def __init__(self, id, author, title, content):
        self.id = id
        self.author = author
        self.title = title
        self.content = content
        self.subSubmissions = {}

    def addSubSubmission(self,submission):
        self.subSubmission[submission,id] = submission
    def getSubSubmission(self,id):
        return self.subSubmission[id]

使用你可以将你的代码更改为

submissions = {}
for sm in reddit.subreddit('redditdev').hot(limit=2):
    submissions[sm.id] = Submission(sm.id, sm.author, sm.title, sm.content)

    # I am not quite sure what these lines are supposed to do, so you might be able to improve these, too
    mySubmission = reddit.submission(id=sm.id)
    mySubmission.comments.replace_more(limit=0)
    for cmt in mySubmission.comments.list():
        submissions[sm.id].addSubSubmission(Submission(cmt.id, cmt.title, cmt.author, cmt.body))

通过使用此apporach,您还可以导出代码以将comments / subSubmissions读出到一个额外的函数中,该函数可以递归调用自身,以便您可以读取注释的深度。