-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwords.py
50 lines (37 loc) · 1.09 KB
/
words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
Usage:
python words.py <URL>
"""
import sys
from urllib.request import urlopen
def fetch_words(url):
"""Fetch a list of words from a URL.
Args:
url: The URL of a UTF-8 text document.
Returns:
A list of strings containing the words from the document.
"""
with urlopen(url) as story:
story_words = []
for line in story:
line_words = line.decode("utf-8").split()
for word in line_words:
story_words.append(word)
return story_words
def print_items(items):
"""Print items one per line.
Args:
An iterable series of printable items.
"""
for item in items:
print(item)
def main(url):
"""Print each word fropm a text document from a URL.
Args:
url: The URL of a UTF-8 text document.
"""
words = fetch_words(url)
print_items(words)
if __name__ == "__main__":
main(sys.argv[1]) # the first argument [0] would be the module filename itself, so [1} is the first additional arg