Python 计算文件中的大写、小写、数字和空白

Python 计算文件中的大写、小写、数字和空白,python,count,character,uppercase,Python,Count,Character,Uppercase,我的任务是计算.txt文件中的大写、小写、数字和空白字符 我试过很多种方法,但似乎都做不好。我不知道我要去哪里 以下是我得到的输出: 大写计数为0 小写计数为0 数字计数为0 空格计数为0 代码: 不要读取行,而是将文件作为一个完整的字符串来读取。在您读取的行中,字符将是一行,但您希望它是字符 不要读取行,而是将文件作为整个字符串读取。如果读取行中的字符将是行,但希望它是字符,则可以使用内置函数 for character in data: if character.isupper():

我的任务是计算.txt文件中的大写、小写、数字和空白字符

我试过很多种方法,但似乎都做不好。我不知道我要去哪里

以下是我得到的输出:

大写计数为0 小写计数为0 数字计数为0 空格计数为0

代码:

不要读取行,而是将文件作为一个完整的字符串来读取。在您读取的行中,字符将是一行,但您希望它是字符


不要读取行,而是将文件作为整个字符串读取。如果读取行中的字符将是行,但希望它是字符,则可以使用内置函数

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
也可能是您可以使用以下命令来计算每一行的字符数

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

您可以简单地使用内置函数

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
也可能是您可以使用以下命令来计算每一行的字符数

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

您正在逐行读取,而不是按字符读取,因此您可以通过读取将其作为整个字符串来读取,另外,最好将所有for循环组合成一个,这样:

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
编辑:

此外,您没有理由像以前一样将所有大写、小写、数字和空格字符存储在列表中,请使用字符串模块,该模块已准备好所有这些字符:

>>> 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 '
更好的方法是使用内置方法:


伊斯洛普岛。。您可以检查它们。

您正在逐行读取,而不是按字符读取,因此您可以通过读取将其作为整个字符串来读取,另外,最好将所有for循环组合成一个,这样:

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
编辑:

此外,您没有理由像以前一样将所有大写、小写、数字和空格字符存储在列表中,请使用字符串模块,该模块已准备好所有这些字符:

>>> 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 '
更好的方法是使用内置方法:


伊斯洛普岛。。您可以检查它们。

或者可以使用内置函数,如isspace、islower、isupper、isdigitOr,也可以使用内置函数,如isspace、islower、isupper、ISDIGITY。您可能有正确答案,但也请提供一些代码说明。您可能有正确答案,但也请提供一些代码说明。