Search code examples
pythongeneratorredditpraw

How to return raw comment data in PRAW instead of generator (Python 3.5)?


I've recently been fiddling around with the PRAW module and the Reddit API for Python 3.

One thing I tried was to retrieve the first of the comments on my account and print it out to the console. My code for this was...

comment = user.get_comments(limit = 1)
print(comment)

Every time it just returns the address:

<generator object get_content at 0x034A5C30>

In order to get the raw string data (the comment itself), I also tried str(), pretty print, and even exporting str(comment) to an external text file. Nothing has produced the results I'm trying to achieve.

I'm new to this API so try to go easy on me. How might I be able to print the comment itself rather than the address, whether it's to the console or to a text file?


Solution

  • get_comments() doesn't return a single comment; it returns an object that you can iterate over to retrieve the comments. You can get the first (and, in this case, only) object from an iterator using the next() function, e.g.

    comment = next(user.get_comments(limit = 1))
    

    What you'll end up with is still a comment object, though. You'll probably want to take comment.body to get the text of the comment.