Count words using python

Python Count Words in File

In this Python tutorial, we will learn about Python Count Words in File. Here we assume the file as a simple Text file (.txt). Also, we will cover these topics.

  • Python Count Words in File
  • Python Count Specific Words in File
  • Python Count Words in Multiple Files
  • Python Count Unique Words Files
  • Python Count Words in Excel File
  • Python Count Unique Words in Text File
  • Python Program to Count Number of Words in File
  • Python Count Word Frequency in a File
  • Python Word Count CSV File

Python Count Words in File

In this section, we will learn about python count words in file. In other words, we will learn to count the total number of words from a text file using Python.

  • The entire process is divided into three simple steps:
    • open a text file in read only mode
    • read the information of file
    • split the sentences into words and find the len.

    Source Code:

    Here is the source code to implement the Python Count Words in a File.

    file = open('file.txt', 'r') read_data = file.read() per_word = read_data.split() print('Total Words:', len(per_word))

    Here is the output of counting words in a file using Python. In this output, the text file we have used has 221 words.

    python count words in a file

    Python Count Specific Words in File

    In this section, we will learn about Python Count Specific Words in File. The user will provide any word and our program will display the total occurrence of that word.

    • Occurrence of specific word can be counted in 5 simple steps:
      • Ask for user input
      • Open the file in read only mode
      • Read the data of the file
      • convert the data in lower case and count the occurrence of specific word
      • Print the count

      Source Code

      Here is the complete source code to perform python count specific words in a file.

      # asking for user input search_word_count = input('Enter the word: ') # opening text file in read only mode file = open("file.txt", "r") # reading data of the file read_data = file.read() # converting data in lower case and the counting the occurrence word_count = read_data.lower().count(search_word_count) # printing word and it's count print(f"The word '' appeared times.") 

      Here is the output of Python Count Specific Word in a File. In this output, we searched for the word ‘the’ in a text file. The result showed that ‘the’ has appeared 4 times in a text file.

      python count specific word

      Python Count Words in Multiple Files

      In this section, we will learn about Python Count Words in Multiple Files. We have three text files that we are going to use and we will count words from all of these files.

      • Counting words from multiple files can be done in five easy steps:
        • import glob module in Python
        • create an empty list to store text files and a counter with 0 as default value.
        • start a loop, recognize the text file using glob and add it to emty list we created in previous step.
        • start another loop on that empty list, total number of files will decide the number of times loop will run. Each time loop runs a file is opened, read, splitted in words and then length of total words in added to words variable.
        • In the end print the word variable with descriptive message.
        for file in glob.glob("*.txt"): txt_files.append(file)
        • In this code, we have started a loop and glob is used to scan all the files with .txt extension.
        • each file is added to an empty list. So everytime loop runs a filename from the current folder having txt extension is added to an empty list.
        for f in txt_files: file = open(f, "r") read_data = file.read() per_word = read_data.split() words += len(per_word)
        • In this code we have started a loop on the empty list because not that empty list has all the text files in it.
        • Each time loop runs a file is opened, read, all the sentences are splitted in words and total count of words are added to a variable.
        • In this way lets say file one has 20 words and file two has 30 then the words variable will show 50 (20+30) words in the end of the loop.
        • print(‘Total Words:’,words) total words are printed with descriptive message.

        Source Code:

        Here is the source code to implement Python Count Words in Multiple Files.

        import glob # empty list and variable txt_files = [] words = 0 # loop to add text files to a list for file in glob.glob("*.txt"): txt_files.append(file) # loop to read, split and count word of each file for f in txt_files: file = open(f, "r") read_data = file.read() per_word = read_data.split() words += len(per_word) # print total words in multiple files print('Total Words:',words)

        Here is the output of the above source code to implement Python Count Words in Multiple Files.

        python count words from multiple files

        Python Count Unique Words in a File

        In this section, we will learn about Python Count Unique Words in a File. The python program will check the occurrences of each word in a text file and then it will count only unique words in a file.

        • Using Python we can count unique words from a file in six simple steps:
          • create a counter and assign default value as zero
          • open a file in read only mode.
          • read the data of file
          • split the data in words and store it in a set
          • start a for loop and keep on incrementing the counter with each word.
          • Ultimately, print the counter

          Source Code:

          Here is the source code for implementing Python Count Unique Words in a File.

          count = 0 file = open("names.txt", "r") read_data = file.read() words = set(read_data.split()) for word in words: count += 1 print('Total Unique Words:', count)

          Here is the output of a program to count unique words in a file using Python. In this output, we have read a file and it has 85 unique words in it.

          python count unique words

          Python Count Words in Excel File

          In this section, we will learn about Python Count Words in Excel File.

          • The best way to count words in excel files using python is by using Pandas module in python.
          • you need to install pandas on your device
          # anaconda conda install pandas # pip pip install pandas
          • Using df.count() method in pandas we can count the total number of words in a file with columns.
          • Using df.count().sum() we can get the final value of total words in a file.
          • Here is the implementation on Jupyter Notebook.

          Python Count Unique Words in Text File

          n this section, we will learn about Python Count Unique Words in a File. The python program will check the occurrences of each word in a text file and then it will count only unique words in a file.

Оцените статью