Difference between revisions of "2010 1.09"

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search
(Created page with "== Read a feed with a URL from the command line == <source lang="python"> #!/usr/bin/env python import sys, feedparser try: url = sys.argv[1] except IndexError: url = "...")
 
Line 13: Line 13:
 
for e in feed.entries:
 
for e in feed.entries:
 
     print e.title.encode("utf-8")
 
     print e.title.encode("utf-8")
 +
</source>
 +
 +
== Word counts ==
 +
 +
<source lang="python">
 +
#!/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
 
</source>
 
</source>

Revision as of 22:21, 6 December 2010

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