当有许多其他实例时,仅打印一个实例

时间:2019-01-31 03:49:55

标签: python python-3.x praw

当尝试将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)

在搜索注释时附加到文件上是否有问题?有什么我想解决的东西吗?

1 个答案:

答案 0 :(得分:0)

尽管我不确定原因,但已经找到了解决方法。

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