0

I need to write a program that reads data from a text file, and prints it. These are the specifications.

And then manipulate the program to determine this:
-The number of uppercase letters in the file
-The number of lowercase letters in the file
-The number of digits in the file
-The number of whitespace characters in the file

I really need help doing this, it has to pertain to strings, but also basic concepts of Python. Im at a begginners skill level, and Im stuck. Much help would be GREATLY appreciated, thank you!

6
Contributors
5
Replies
29
Views
6 Years
Discussion Span
Last Post by Reverend Jim
0

Good luck, we are always ready to help, you only have to post in your code and explain where you have difficulty.

1

I'm having this problem as well. I don't need to print the file, I just need to analyze. Here's my code.

def main():
    file=input("Enter the filename: ")
    infile = open(file,'r')
    cap_count=0
    low_count=0
    space_count=0
    dig_count=0
    for ch in infile:
        if ch.isupper():
            cap_count+=1
        elif ch.islower():
            low_count+=1
        elif ch.isspace():
            space_count+=1
        elif ch.isdigit():
            dig_count+=1
    print(cap_count, low_count, space_count, dig_count)
main()
0

I don't know if this is was what you wanted :)

def main():

    infile = open('whatever.txt','r')
    file = infile.readlines()
    cap_count=0
    low_count=0
    space_count=0
    dig_count=0
    for lines in file:
        for ch in lines:
            if ch.isupper():
                cap_count+=1
            elif ch.islower():
                low_count+=1
            elif ch.isspace():
                space_count+=1
            elif ch.isdigit():
                dig_count+=1
    print(cap_count, low_count, space_count, dig_count)
main()
2

I see that the print statement uses parentheses which tells me this is 3.x code so it should work. Is it too much work for you to copy/paste the code and try running it yourself?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.