Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PRAW使用3+;将子Reddit中的注释添加到列表中_Python_Praw - Fatal编程技术网

Python PRAW使用3+;将子Reddit中的注释添加到列表中

Python PRAW使用3+;将子Reddit中的注释添加到列表中,python,praw,Python,Praw,我目前有一个使用PRAW编写的Python reddit bot,它从一个特定的子reddit中获取所有注释,查看其作者,并找出该作者在子reddit中是否至少有3条注释。如果他们有3+条评论,则会将其添加到“批准后提交”文本文件中。 我的代码目前“工作”,但老实说,它是如此糟糕,我甚至不知道如何。实现目标的更好方法是什么? 我目前拥有的: def get_approved_posters(reddit): subreddit = reddit.subreddit('Enter_Subr

我目前有一个使用PRAW编写的Python reddit bot,它从一个特定的子reddit中获取所有注释,查看其作者,并找出该作者在子reddit中是否至少有3条注释。如果他们有3+条评论,则会将其添加到“批准后提交”文本文件中。 我的代码目前“工作”,但老实说,它是如此糟糕,我甚至不知道如何。实现目标的更好方法是什么? 我目前拥有的:

def get_approved_posters(reddit):

   subreddit = reddit.subreddit('Enter_Subreddit_Here')
   subreddit_comments = subreddit.comments(limit=1000)
   dictionary_of_posters = {}
   unique_posters = {}
   commentCount = 1
   duplicate_check = False
   unique_authors_file = open("unique_posters.txt", "w")

   print("Obtaining comments...")
   for comment in subreddit_comments:
      dictionary_of_posters[commentCount] = str(comment.author)
      for key, value in dictionary_of_posters.items():
        if value not in unique_posters.values():
            unique_posters[key] = value
    for key, value in unique_posters.items():
        if key >= 3:
            commentCount += 1
        if duplicate_check is not True:
            commentCount += 1
            print("Adding author to dictionary of posters...")
            unique_posters[commentCount] = str(comment.author)
            print("Author added to dictionary of posters.")
            if commentCount >= 3:
                duplicate_check = True

   for x in unique_posters:
      unique_authors_file.write(str(unique_posters[x]) + '\n')

   total_comments = open("total_comments.txt", "w")
   total_comments.write(str(dictionary_of_posters))

   unique_authors_file.close()
   total_comments.close()

   unique_authors_file = open("unique_posters.txt", "r+")
   total_comments = open("total_comments.txt", "r")
   data = total_comments.read()
   approved_list = unique_authors_file.read().split('\n')
   print(approved_list)
   approved_posters = open("approved_posters.txt", "w")
   for username in approved_list:
      count = data.count(username)
      if(count >= 3):
        approved_posters.write(username + '\n')
      print("Count for " + username + " is " + str(count))

   approved_posters.close()
   unique_authors_file.close()
   total_comments.close()

也许只是我今天早上反应迟钝,但我很难理解你对commentCount和unique_海报的使用。实际上,可能是我

我会像您一样从subreddit获取所有评论,对于每个评论,请执行以下操作:

for comment in subreddit_comments:
    try:
        dictionary_of_posters[comment.author] += 1
    except KeyError:
        dictionary_of_posters[comment.author] = 1

for username, comment_count in dictionary_of_posters.items():
    if comment_count >= 3:
        approved_authors.append(username)

此方法利用了一个事实,即字典不能有两个相同的键值。这样,你就不必做重复检查或任何事情。如果它让你感觉更好,你可以去
列表(set(approved_authors))
,这将消除任何多余的重复项。

这段代码是不是在一个函数中?你应该缩进,这样我们就可以知道函数中有什么和没有什么;我会编辑它你希望得到什么样的改进?速度精确这个代码对你来说有什么“错误”吗?关于我们如何帮助你,没有太多的指导。我目前正在使用3个不同的文本文件,只是为了找出谁在subreddit中发布了3次。它也不总是准确的。我最初在一个特定的子Reddit中使用它,它工作正常,然后我在第二个子Reddit上测试了代码,结果在我的输出文件中连续三次使用相同的名称,而不是唯一的文本文件,我会将您的数据存储在json文件中。因此,您可以将数据添加到一个字典中,该字典按键细分为需要跟踪的三种数据类型,然后将其转换为json字符串并将其全部转储到一个文件中。关于它有很多教程,我个人
导入json
并使用
json.load()
json.dump()
。我会继续查看你的代码,看看还有什么可以帮助你