Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys, re
- def main(argv):
- if len(argv) != 1:
- print("Tiedostonnimi puuttuu!")
- exit(0)
- try:
- with open(argv[0]) as file:
- words_count = 0; letters_count = 0; move_to_next_line = ""
- for line in file:
- splitted = (line.rstrip().replace("\t", " ")).split()
- words_max = len(splitted)
- if words_max > 0:
- splitted[0] = move_to_next_line + splitted[0]
- move_to_next_line = ""
- last_word = splitted[words_max-1]
- if last_word[-1] == "-":
- move_to_next_line = last_word
- words_max -= 1
- for i in range(words_max):
- word = "".join(re.split(r"!|\?|–|-|,|'|:|;|\.", splitted[i]))
- if word.isalpha():
- words_count += 1
- letters_count += len(word)
- print("Sanoja:", words_count, "Kirjaimia:", letters_count)
- except OSError as e:
- print(e)
- if __name__ == "__main__":
- main(sys.argv[1:])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement