David
David

Reputation: 157

counting letters in a text file in python

So I'm trying to do this problem

Write a program that reads a file named text.txt and prints the following to the screen:

 The number of characters in that file

 The number of letters in that file

 The number of uppercase letters in that file

 The number of vowels in that file

I have gotten this so far but I am stuck on step 2 this is what I got so far.

file = open('text.txt', 'r')
lineC = 0
chC = 0
lowC = 0
vowC = 0
capsC = 0
for line in file:
    for ch in line:
        words = line.split()
        lineC += 1
        chC += len(ch)
for letters in file:
        for ch in line:
print("Charcter Count = " + str(chC))
print("Letter Count = " + str(num))

Upvotes: 1

Views: 6028

Answers (2)

Brian H.
Brian H.

Reputation: 2225

The answer above uses regular expressions, which are very useful and worth learning about if you haven't used them before. Bunji's code is also more efficient, as looping through characters in a string in Python is relatively slow.

However, if you want to try doing this using just Python, take a look at the code below. A couple of points: First, wrap your open() inside a using statement, which will automatically call close() on the file when you are finished. Next, notice that Python lets you use the in keyword in all kinds of interesting ways. Anything that is a sequence can be "in-ed", including strings. You could replace all of the string.xxx lines with your own string if you would like.

import string

chars = []

with open("notes.txt", "r") as f:
    for c in f.read():
        chars.append(c)


num_chars = len(chars)

num_upper = 0;
num_vowels = 0;
num_letters = 0

vowels = "aeiouAEIOU"
for c in chars:
    if c in vowels:
        num_vowels += 1
    if c in string.ascii_uppercase:
        num_upper += 1
    if c in string.ascii_letters:
        num_letters += 1

print(num_chars)
print(num_letters)
print(num_upper)
print(num_vowels)

Upvotes: 2

bunji
bunji

Reputation: 5213

You can do this using regular expressions. Find all occurrences of your pattern as your list and then finding the length of that list.

import re
with open('text.txt') as f:
    text = f.read()
    characters = len(re.findall('\S', text))
    letters = len(re.findall('[A-Za-z]', text))
    uppercase = len(re.findall('[A-Z]', text))
    vowels = len(re.findall('[AEIOUYaeiouy]', text))

Upvotes: 3

Related Questions