从文件中读取字数并计算每个句子的平均值的有效方法

时间:2017-12-19 07:05:39

标签: python file word-count line-count

我需要编写一个python代码来读取文本文件(file.txt)的内容并计算每个句子的平均单词数。(假设文件中每行只包含一个句子。)

我做了编码,我需要知道它是否可以通过其他方式提高效率。百万提前谢谢。 这是我的:

# This program reads contents of a .txt file and calulate
# the average number of words per sentence .

line_count=0
# open the file.txt for reading
content_file=open('file.txt','r')

# calculate the word count of the file
content=content_file.read()

words= content.split()

word_count=len(words)

# calculate the line count
for line in open('file.txt'):

    line_count+=1

content_file.close()

# calculate the average words per line

average_words=word_count/line_count

# Display the result

print('The average word count per sentence is', int(average_words))

3 个答案:

答案 0 :(得分:0)

无需迭代文件两次。只需通过以下行来更新计数::

lc, wc = 0, 0
with open('file.txt','r') as f:
    for line in f:
        lc += 1
        wc += len(line.strip().split())

avg = wc / lc

答案 1 :(得分:0)

我的建议是,而不是使用for循环将内容拆分为'\ n'并找到数组的长度。

打开file.txt进行阅读

content_file =开放( 'file.txt的', 'R')

计算文件的字数

含量= content_file.read()

WORD_COUNT = LEN(content.split())

line_count = len(content.split('\ n'))

content_file.close()

计算每行的平均字数

average_words = WORD_COUNT / LINE_COUNT

显示结果

print('每个句子的平均单词数为',int(average_words))

答案 2 :(得分:0)

以下代码将是高效的,因为我们一次只读取一次文件内容。

with open(r'C:\Users\lg49242\Desktop\file.txt','r') as content:
    lineCount = 0
    Tot_wordCount = 0
    lines = content.readlines()
    for line in lines:
        lineCount = lineCount + 1       
        wordCount = len(line.split())
        Tot_wordCount += wordCount

avg = Tot_wordCount / lineCount

print avg