Word count

Write a python program that prints number of characters, words and lines in a file. For example, when run on diamond.txt:
Diamond has remarkable
optical characteristics.
Because of its extremely
rigid lattice, it can be
contaminated by very few
types of impurities,
such as boron and
nitrogen. Combined with
wide transparency, this
results in the clear,
colorless appearance of
most natural diamonds.
... it should print the number of lines, words and characters in the file.

Sample output

12 41 279
 
 

best solution

 
 
import string
data = open('diamond.txt', 'r').read()
print len(data.splitlines()), len(string.split(data)), len(data) 



No comments:

Post a Comment