Advertisement
Guest User

wc.py

a guest
Aug 28th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. import io
  2.  
  3.  
  4. def wc(path):
  5.     with open(path, "r", encoding="utf-8") as f:
  6.         lines = words = chars = 0
  7.         for line in f:
  8.             lines += 1
  9.             words += len(line.split())
  10.             chars += len(line)
  11.     return lines, words, chars
  12.  
  13.  
  14. def wc_str(string):
  15.     f = io.StringIO(string)
  16.     lines = words = chars = 0
  17.     for line in f:
  18.         lines += 1
  19.         words += len(line.split())
  20.         chars += len(line)
  21.     return lines, words, chars
  22.  
  23.  
  24. def wc_split(string):
  25.     f = string.split('\n')
  26.     lines = words = chars = 0
  27.     for line in f:
  28.         lines += 1
  29.         words += len(line.split())
  30.         chars += len(line)
  31.     return lines, words, chars
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement