Advertisement
Guest User

Untitled

a guest
Oct 31st, 2021
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. import sys, re
  4.  
  5. def main(argv):
  6.    if len(argv) != 1:
  7.       print("Tiedostonnimi puuttuu!")
  8.       exit(0)
  9.    try:
  10.       with open(argv[0]) as file:
  11.          words_count = 0; letters_count = 0; move_to_next_line = ""
  12.          for line in file:
  13.             splitted = (line.rstrip().replace("\t", " ")).split()
  14.             words_max = len(splitted)
  15.             if words_max > 0:
  16.                splitted[0] = move_to_next_line + splitted[0]
  17.                move_to_next_line = ""
  18.                last_word = splitted[words_max-1]
  19.                if last_word[-1] == "-":
  20.                   move_to_next_line = last_word
  21.                   words_max -= 1
  22.                for i in range(words_max):
  23.                   word = "".join(re.split(r"!|\?|–|-|,|'|:|;|\.", splitted[i]))
  24.                   if word.isalpha():
  25.                      words_count += 1
  26.                      letters_count += len(word)
  27.          print("Sanoja:", words_count, "Kirjaimia:", letters_count)
  28.    except OSError as e:
  29.       print(e)
  30.  
  31. if __name__ == "__main__":
  32.    main(sys.argv[1:])
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement