-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_and_words.py
92 lines (70 loc) · 2.44 KB
/
string_and_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import sys
import re
import string
import operator
def word_count(content):
content = content.translate(str.maketrans('', '', string.punctuation)).split()
word_count = {}
for s in content:
if s not in word_count.keys():
word_count[s] = 1
else:
word_count[s] += 1
return word_count
def word_replacement(old, new, content):
return re.sub(r"\b%s\b" % old, new, content)
def grep_line(word, content):
digits = len(str(len(content))) - 1
n = 0
r = []
for s in content.splitlines():
n += 1
if re.search(r"\b%s\b" % word, s):
r.append("{n:{d}}: {s}".format(n=n, d=digits, s=s.strip()))
return r
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Error: Not enough arguments.")
sys.exit(4)
# word count
elif sys.argv[1] == '-c':
if len(sys.argv) < 3:
print("Error: Not enough arguments to perform word count.", file=sys.stderr)
sys.exit(1)
try:
with open(sys.argv[2], 'r') as f:
split = f.read()
except FileNotFoundError:
print("Error: File %s not found." % sys.argv[2], file=sys.stderr)
sys.exit(2)
sorted_count = sorted(word_count(split).items(), reverse=True, key=operator.itemgetter(1))
for k in sorted_count:
print("%s: %d" % (k[0], k[1]))
# word replacement
elif sys.argv[1] == '-r':
if len(sys.argv) < 6:
print("Error: Not enough arguments to perform word replacment.", file=sys.stderr)
sys.exit(3)
try:
with open(sys.argv[4], 'r') as f:
content = f.read()
except FileNotFoundError:
print("Error: File %s not found." % sys.argv[4], file=sys.stderr)
sys.exit(2)
replacement = word_replacement(sys.argv[2], sys.argv[3], content)
with open(sys.argv[5], 'w') as f:
f.write(replacement)
# grepline
elif sys.argv[1] == '-g':
try:
with open(sys.argv[3], 'r') as f:
p = f.read()
except FileNotFoundError:
print("Error: File %s not found." % sys.argv[3], file=sys.stderr)
sys.exit(2)
except IndexError:
print("Error: Not enough arguments to perform grepline.", file=sys.stderr)
sys.exit(3)
g = grep_line(sys.argv[2], p)
for s in g:
print(s)