首页 > 解决方案 > 当有许多其他实例时,只打印一个实例

问题描述

尝试附加comment到时,尽管有许多其他实例,但x仅打印了一个实例。comment

    with open("testingusercomments.txt", "r") as a, open("testingusercommentstmp.txt", "a") as x:
        try:
            for comment in r.redditor("username").comments.new(limit=None):
                if comment not in a.read().split("\n"):
                    print(comment)
                    x.append(comment)

尽管此代码仅返回以下代码的一个实例,comment但返回了适当的数量。

with open("testingusercomments.txt", "r") as a, open("testingusercommentstmp.txt", "a") as x:
    try:
        for comment in r.redditor("username").comments.new(limit=None):
            if comment not in a.read().split("\n"):
                print(comment)

搜索评论时附加到文件是否有问题?有什么我想念的东西来解决这个问题吗?

标签: pythonpython-3.xpraw

解决方案


虽然我不确定原因,但我找到了解决方案。

x = []
for comment in r.redditor("username").comments.new(limit=None):
    if comment not in x:
        x.append(comment)

推荐阅读