Skip to content
This repository was archived by the owner on May 25, 2022. It is now read-only.

Commit 8facac4

Browse files
中尾吏志中尾吏志
authored andcommitted
add some return value
1 parent 24dc5b1 commit 8facac4

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

projects/Textfile analysis/textfile_analysis.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
import os
33
import sys
44
import collections
5+
import string
6+
import re
57

68
script_name = sys.argv[0]
79

@@ -15,15 +17,17 @@
1517

1618
try:
1719
textfile = sys.argv[1]
18-
with open(textfile) as f:
19-
data = f.read()
20+
with open(textfile, "r", encoding = "utf_8") as f:
2021

22+
data = f.read()
2123
res["total_lines"] = data.count(os.linesep)
22-
res["total_characters"] = len(data)
24+
res["total_characters"] = len(data.replace(" ","")) - res["total_lines"]
2325
counter = collections.Counter(data.split())
2426
d = counter.most_common()
2527
res["total_words"] = sum([i[1] for i in d])
2628
res["unique_words"] = len([i[0] for i in d])
29+
special_chars = string.punctuation
30+
res["special_characters"] = sum(v for k, v in collections.Counter(data).items() if k in special_chars)
2731

2832
except IndexError:
2933
print('Usage: %s TEXTFILE' % script_name)

0 commit comments

Comments
 (0)