- Read the text file.text = open("filename.txt").read()
- Replace non-alphanumeric characters as a whitespace.import re text = re.sub('[^w&^d]', ' ', text)
- Change all characters to lowercase.text = text.lower()
- Split words into a list.text = text.split()
- Display the number for words in the text file.len(text)
- Display the number of unique words in the text file.len(set(text))
- Display the number of occurrences for each word.from collections import defaultdict
wordsCount = defaultdict(int)
for word in text:
wordsCount[word] += 1
for word, num in wordsCount.items():
print(word, num)
Showing posts with label file. Show all posts
Showing posts with label file. Show all posts
Monday, June 1, 2009
Count the number of words using Python
This article represents a way to count the number of words, the number of unique words, and the number of each word occurrences in a text file.
Labels:
collections
,
count
,
defaultdict
,
defaultdict.items
,
file
,
len
,
number
,
open
,
Python
,
re
,
read
,
Set
,
words
Saturday, May 9, 2009
How to know what software to use to open a file
Do you know what software to use to open a .gbr file or .pps file?
If it is a problem for you, OpenWith.org Desktop Tool, is what you need.
This software connects to online database of huge number of file extensions.
It will advice you on what software to use in order to open a file, by right clicking the file, and choose "OpenWith.org - How do I open this?"
OpenWith.org Desktop Tool is compatible with 98, ME, 2000, XP and Vista.
If it is a problem for you, OpenWith.org Desktop Tool, is what you need.
This software connects to online database of huge number of file extensions.
It will advice you on what software to use in order to open a file, by right clicking the file, and choose "OpenWith.org - How do I open this?"
OpenWith.org Desktop Tool is compatible with 98, ME, 2000, XP and Vista.
Subscribe to:
Posts
(
Atom
)