【问题标题】:Count uppercase, lowercase, digits, and whitespace in file计算文件中的大写、小写、数字和空格
【发布时间】:2016-03-11 15:33:09
【问题描述】:

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

我已经尝试了多种方法,但似乎都无法做到正确。我不知道我在哪里。

这是我得到的输出:

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

代码:

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()

【问题讨论】:

    标签: python count character uppercase


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      你是逐行阅读,而不是char,所以你通过read将它作为一个完整的字符串阅读,另外最好将你所有的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
      

      编辑:

      此外,您没有理由像以前那样在列表中存储所有 upper-caselower-casedigitswhite space 字符,请使用 string 模块,它已经准备好所有这些:

      >>> 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 '
      

      更好的方法是使用内置方法:

      isupper(), islower() .. 你可以检查它们here.

      【讨论】:

      • 或者可以使用内置函数,如 isspace、islower、isupper、isdigit
      【解决方案3】:
      data = infile.read()
      

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

      【讨论】:

        【解决方案4】:
        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)))
        

        【讨论】:

        • 您可能有正确的答案,但也请提供您的代码的一些解释。
        猜你喜欢
        • 2016-04-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-04
        • 1970-01-01
        • 1970-01-01
        • 2014-10-03
        相关资源
        最近更新 更多