2010 1.09: Difference between revisions
No edit summary |
No edit summary |
||
Line 15: | Line 15: | ||
print e.title.encode("utf-8") | print e.title.encode("utf-8") | ||
</source> | </source> | ||
<source lang="python"> | |||
#!/usr/bin/env python | |||
from optparse import OptionParser | |||
parser = OptionParser() | |||
parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co.uk/news/rss.xml", help="the url to read from") | |||
parser.add_option("-n", "--numlines", type="int", dest="num", default=1000, help="how many lines to display") | |||
(options, args) = parser.parse_args() | |||
import feedparser | |||
feed = feedparser.parse(options.url) | |||
for e in feed.entries[:options.num]: | |||
print e.title.encode("utf-8") | |||
</source> | |||
== Words == | == Words == |
Revision as of 15:07, 7 December 2010
Display the titles of an RSS feed
#!/usr/bin/env python
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co.uk/news/rss.xml", help="the url to read from")
parser.add_option("-n", "--numlines", type="int", dest="num", default=1000, help="how many lines to display")
(options, args) = parser.parse_args()
import feedparser
feed = feedparser.parse(options.url)
for e in feed.entries[:options.num]:
print e.title.encode("utf-8")
#!/usr/bin/env python
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-u", "--url", dest="url", default="http://feeds.bbci.co.uk/news/rss.xml", help="the url to read from")
parser.add_option("-n", "--numlines", type="int", dest="num", default=1000, help="how many lines to display")
(options, args) = parser.parse_args()
import feedparser
feed = feedparser.parse(options.url)
for e in feed.entries[:options.num]:
print e.title.encode("utf-8")
Words
Turns a text in an alphabetical list of unique words. Attempts to strip punctuation and lowercases everything.
#!/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 in sorted(words.keys()):
print word,
print
Word counts
Grab words (as above) and display one per line followed by the number of times the word appears.
#!/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
Permutations
bla bla
#!/usr/bin/python
import sys, codecs, string, random
sys.stdin = codecs.getreader("utf-8")(sys.stdin)
count = {}
for line in sys.stdin:
for w in line.split():
w = w.strip(string.punctuation).lower()
if w:
count[w] = count.get(w,0)+1
#for w,c in sorted(count.items()):
# print w,c
words = count.keys()
#words.sort()
random.shuffle(words)
words = words[0:5]
import itertools
for ws in itertools.permutations(words):
for w in ws:
print w,
print