2010 1.09

From XPUB & Lens-Based wiki
Revision as of 23:21, 6 December 2010 by Michael Murtaugh (talk | contribs)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Read a feed with a URL from the command line

#!/usr/bin/env python
import sys, feedparser

try:
    url = sys.argv[1]
except IndexError:
    url = "http://feeds.bbci.co.uk/news/rss.xml"

feed = feedparser.parse(url)
for e in feed.entries:
    print e.title.encode("utf-8")

Word counts

#!/usr/bin/env python

import sys, string

words = {}
for line in sys.stdin:
    for word in line.split():
        word = word.lower().strip(string.punctuation)
        words[word] = words.get(word, 0) + 1

for (word, count) in sorted(words.items()):
    print word, count