📜  pyton 计算文本文件中的语义 - Python 代码示例

📅  最后修改于: 2022-03-11 14:45:18.848000             🧑  作者: Mango

代码示例1
fname = "gettysburg.txt"

num_lines = 0
num_words = 0
num_chars = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()

        num_lines += line.count(".")
        num_words += len(words)
        num_chars += len(line.replace(' ', ''))