类型错误:无法连接'str'和'Redditor'对象

3
Traceback (most recent call last):
  File "run.py", line 56, in <module>
    run(r, comments_replied_to)
  File "run.py", line 30, in run_bot
    b.write("Author=" + comment.submission.author + "\n")
TypeError: cannot concatenate 'str' and 'Redditor' objects

这是从代码中接收到的回溯信息,我不确定该如何解决,谢谢。
以下是代码:
with open ("first.txt", "a") as f, open 
    ("second.txt", "a") as b:
        f.write(comment.id + "\n")
            b.write("author=" + comment.submission.author + "\n")

请提供一些原始代码的示例,以便参考此错误。 - Newskooler
你不能连接字符串和对象。 - vidstige
1个回答

3
我猜你正在使用PRAW并想获取reddit用户名?目前,你正试图将字符串与Redditor对象拼接。
相反,你需要使用"Redditor.name"来访问用户名。查看Redditor对象的文档。
在你的情况下,应该这样做:
comment.submission.author.name

我正在寻找编写提交者(帖子而非评论的)用户名到文本文件中的脚本。First.txt 运行良好,但 second.txt 出现错误。 - Dann
1
那绝对解决了问题,谢谢。我不知道我的comment.submission.author的使用会导致错误。 - Dann
那绝对有道理,再看一眼 https://praw.readthedocs.io/en/latest/code_overview/models/comment.html 会有所帮助。 - Dann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接