如何读取Python中的字符数,行数和单词数

时间:2015-04-14 11:49:55

标签: python lines words

from string import lowercase
from itertools import groupby

lines = [line.strip() for line in open('diamond.txt')] 
count=0
sumofwords=0
letters=[]

def numberoflines(lines):
       return len(lines)

def numberofletters(letters):
       count=0
       for l in letters:
             for ll in l:
               count=count+len(ll)
       return count

for s in lines:
      count=count+1
      counts = [(len(list(cpart))) for c,cpart in groupby(s) if c == ' ']    
      sumofwords=sumofwords+len(counts)+1
      letters.append(s.split())

print "Number of lines in document ",numberoflines(lines),"\n"    
print "Number of words in document ",sumofwords,"\n"
print "Number of letters in document ",numberofletters(letters)

这是代码,我需要在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.

行和单词给出正确的输出,但文档中的字母数量为238。

正确的输出应该是279。

3 个答案:

答案 0 :(得分:0)

一个简单的解决方案

lines = [line for line in open('diamond.txt')]
wordscnt = 0
lettercnt = 0
for s in lines:
    wordscnt+=len(s.split())
    lettercnt+=len(s)
print "Number of lines in document ",len(lines),"\n"
print "Number of words in document ",wordscnt,"\n"
print "Number of letters in document ",lettercnt

或更短的解决方案

lines = [line for line in open('diamond.txt')]
wordscnt = sum( len(l.split()) for l in lines)
lettercnt = sum( len(l) for l in lines)
print "Number of lines in document ",len(lines),"\n"
print "Number of words in document ",wordscnt,"\n"
print "Number of letters in document ",lettercnt

答案 1 :(得分:0)

你在分割线条后计算字母,意味着你不计算空格。 你可以简单地尝试用len(text)作为字母数。

答案 2 :(得分:0)

我认为你混合了没有空格的字母数量实际上是238,而你期望的答案是整个文本的长度,包括空格,这是279,顺便说一句,我更喜欢你使用str .replace()删除文本中的空格