Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python count words in file

import re
# Open file in read mode
input_file = open("C:UsersfawazDesktopdata.txt", "rt")
"""
Get each line from file
Then decompose it into words
Update count with nb of words
per line.
"""
count = 0
for line in input_file: 
	words = re.split("s+", line.strip())  # words separated by any nb of white spaces
	print(words)
	count += len(words)

print("Number of words in input file:", count)

input_file.close()
Comment

PREVIOUS NEXT
Code Example
Python :: python get newest file in directory 
Python :: install googlesearch for python 
Python :: python barcode generator 
Python :: run celery on windows 
Python :: stripping /n in a readlines for a pytgon file 
Python :: python requests get title 
Python :: print numpy version 
Python :: How to print list without for loop python 
Python :: frequency count of values in pandas dataframe 
Python :: remove r and n from string python 
Python :: print type of exception python 
Python :: remove web linnks from string python 
Python :: python remove empty string from list 
Python :: load custom font pygame 
Python :: cannot remove column in pandas 
Python :: how to send whatsapp message with python 
Python :: jupyter notebook pass python variable to shell 
Python :: tkinter image 
Python :: save crontab python to file 
Python :: discord.py commands not working 
Python :: how to find wifi password using python 
Python :: qtimer python 
Python :: python get ros package path 
Python :: get current time in python with strftime 
Python :: python sys is not defined 
Python :: python merge pdfs 
Python :: how to make turtle invisible python 
Python :: apply format to pandas datetime column 
Python :: python setup.py bdist_wheel did not run successfully 
Python :: how to apply logarithm in pandas dataframe 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =