-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_processor.py
executable file
·63 lines (48 loc) · 1.5 KB
/
text_processor.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
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""Retrieve and print words from a URL.
Usage:
python3 text_processor.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 UTF-8-decoded 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 six per line with a space in between each item
Args:
items: an iterable series that can be parsed as a string
"""
word_list = ''
word_cursor = 0
print("Word Count", len(items))
while word_cursor < len(items):
paragraphCursor = 0
while paragraphCursor < 6:
if (word_cursor + paragraphCursor) == len(items):
break
word_list += str(items[word_cursor + paragraphCursor])
word_list += ' '
paragraphCursor += 1
word_cursor += paragraphCursor
word_list += '\n'
print(word_list)
def main(url):
"""Print each word from a text document from a URL.
Args:
url: The URL to a UTF-8 text document
"""
print_items(fetch_words(url))
if __name__ == '__main__':
main(sys.argv[1]) # The 0th arg is the module filename