📌  相关文章
📜  计算文本文件中单词的Python程序

📅  最后修改于: 2022-05-13 01:55:33.690000             🧑  作者: Mango

计算文本文件中单词的Python程序

在本文中,我们将了解如何使用Python计算文本文件中的单词数。

示例 1:计算字符串字数

首先,我们创建一个文本文件,我们要计算其中的单词数。将此文件设为 SampleFile.txt,内容如下:

演示文件

下面是实现:

Python3
# creating variable to store the
# number of words
number_of_words = 0
  
# Opening our text file in read only
# mode using the open() function
with open(r'SampleFile.txt','r') as file:
  
    # Reading the content of the file
    # using the read() function and storing
    # them in a new variable
    data = file.read()
  
    # Splitting the data into seperate lines
    # using the split() function
    lines = data.split()
  
    # Adding the length of the
    # lines in our number_of_words
    # variable
    number_of_words += len(lines)
  
  
# Printing total number of words
print(number_of_words)


Python3
# creating variable to store the
# number of words
number_of_words = 0
  
# Opening our text file in read only
# mode using the open() function
with open(r'SampleFile.txt','r') as file:
  
    # Reading the content of the file
    # using the read() function and storing
    # them in a new variable
    data = file.read()
  
    # Splitting the data into seperate lines
    # using the split() function
    lines = data.split()
  
    # Iterating over every word in
    # lines
    for word in lines:
  
        # checking if the word is numeric or not
        if not word.isnumeric():          
  
            # Adding the length of the
            # lines in our number_of_words
            # variable
            number_of_words += 1
  
# Printing total number of words
print(number_of_words)


输出:



7

解释:

  • 创建一个新变量来存储文本文件中的总字数。然后使用 open()函数以只读模式打开文本文件。
  • 使用 read()函数读取文件内容并将它们存储在一个新变量中。然后使用 split()函数将存储在 data 变量中的数据拆分为单独的行,然后将它们存储在一个新变量中。并在 number_of_words 变量中添加行的长度。

示例 2:统计单词的数量,而不是 Integer

演示文件:

下面是实现:

蟒蛇3

# creating variable to store the
# number of words
number_of_words = 0
  
# Opening our text file in read only
# mode using the open() function
with open(r'SampleFile.txt','r') as file:
  
    # Reading the content of the file
    # using the read() function and storing
    # them in a new variable
    data = file.read()
  
    # Splitting the data into seperate lines
    # using the split() function
    lines = data.split()
  
    # Iterating over every word in
    # lines
    for word in lines:
  
        # checking if the word is numeric or not
        if not word.isnumeric():          
  
            # Adding the length of the
            # lines in our number_of_words
            # variable
            number_of_words += 1
  
# Printing total number of words
print(number_of_words)

输出:

11

说明:创建一个新变量来存储文本文件中的总字数,然后使用 open()函数以只读模式打开文本文件。使用 read()函数读取文件内容并将它们存储在一个新变量中,然后使用 split()函数将存储在 data 变量中的数据拆分为单独的行,然后将它们存储在一个新变量中,迭代每个使用 for 循环在行中输入单词并使用 isnumeric()函数检查单词是否为数字,然后在我们的 number_of_words 变量中加 1。