Reputation: 21
I'm tasked with counting uppercase, lowercase, digits, and whitespace characters in a .txt
file.
I've tried it a variety of ways and I can't quite seem to get it right. I can't figure out where I'm off.
Here's the output that I get:
The uppercase count is 0 The lowercase count is 0 The digit count is 0 The whitespace count is 0
Code:
def main():
uppercase_count = 0
lowercase_count = 0
digits_count = 0
whitespace_count = 0
uppercase =['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
lowercase = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
digits = ['0','1','2','3','4','5','6','7','8','9']
whitespace = [' ']
infile = open("text.txt", "r")
data = infile.readlines()
for character in data:
if character in uppercase:
uppercase_count += 1
for character in data:
if character in lowercase:
lowercase_count += 1
for character in data:
if character in digits:
digits_count += 1
for character in data:
if character in whitespace:
whitespace_count += 1
print('The uppercase count is',uppercase_count)
print('The lowercase count is',lowercase_count)
print('The digit count is',digits_count)
print('The whitespace count is',whitespace_count)
main()
Upvotes: 1
Views: 15248
Reputation: 11
data = infile.read()
value = [ i for i in data]
upper = len(list(filter(lambda x: x.isupper(), value)))
digit = len(list(filter(lambda x: x.isdigit(), value)))
space = len(list(filter(lambda x: x.isspace(), value)))
Upvotes: 1
Reputation: 3267
You could simply use built in fuctions
for character in data:
if character.isupper():
uppercase_count += 1
elif character.islower():
lowercase_count += 1
elif character.isspace():
whitespace_count +=1
elif character.isdigit():
digit_count +=1
Also may be you could use the following to have each line get counted for characters.
digit_count,whitespace_count,lowercase_count,uppercase_count=0,0,0,0
lines = infile.readlines()
for data in lines:
for character in data:
if character.isupper():
uppercase_count += 1
elif character.islower():
lowercase_count += 1
elif character.isspace():
whitespace_count +=1
elif character.isdigit():
digit_count +=1
Upvotes: 3
Reputation: 10951
You are reading line by line, not by char
, so you read it as a whole string through read
, plus it's better to group all your for
loops into one, this way:
data = infile.read()
for character in data:
if character in uppercase:
uppercase_count += 1
elif character in lowercase:
lowercase_count += 1
elif character in digits:
digits_count += 1
elif character in whitespace:
whitespace_count += 1
EDIT:
Also, there is no reason for you to store all upper-case
, lower-case
, digits
and white space
characters in lists like you did, use the string
module, which has all of these already ready:
>>> string.uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8a\x8c\x8e\x9f\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\xdb\xdc\xdd\xde'
>>> string.lowercase
'abcdefghijklmnopqrstuvwxyz\x83\x9a\x9c\x9e\xaa\xb5\xba\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'
>>> string.whitespace
'\t\n\x0b\x0c\r '
A better approach is to use built-in methods:
isupper(), islower() .. which you can check for them here.
Upvotes: 2
Reputation: 67968
data = infile.read()
Do not read lines
but read the file as a whole string.Of you read lines character
will be a line
but you want it to be character
Upvotes: 1