Search code examples
pythonprawgeektool

GeekTool only iterates through my python loop once


I built a very simple script with PRAW that prints the top 10 link titles on reddit.com/r/worldnews. I want this to work with GeekTool, but only the following shows up:

"TOP 10 NEWS ON REDDIT

1 NEWS TITLE

2 "

I don't know why that happens since when running the script directly from the command line I have no issues whatsoever.

Here's the python script:

import praw

def main():
    subreddit = r.get_subreddit('worldnews')
    x = 1
    print "TOP 10 NEWS ON REDDIT"
    print '' 
    for submission in subreddit.get_hot(limit=10):
        print x, submission.title
        x = x+1
        print ' '

if __name__ == "__main__":
    user_agent = "Top10 0.1 by /u/alexisfg"
    r = praw.Reddit(user_agent=user_agent)
    main()

Solution

  • If you put a try...except around the main function to print any exceptions, you get the following error message:

    ascii codec can't encode character u'\u2019' in position 12: ordinal not in range(128)
    

    So this is an encoding issue - some character in the second title is not in the ASCII range, which python/Geektool is using as the default encoding. You can get around this by encoding the title string explicitly with .encode('utf-8').