繁体   English   中英

如何使用JES(适用于学生的Python)对文件中的字符进行计数

[英]How to count characters in a file using JES (Python for students)

如何有蟒蛇读取文本文件,并返回的数linescharactersvowelsconsonantslowercase lettersuppercase letters

编写一个程序,该程序接受文件名作为命令行参数。 (您可以假定输入文件将是纯文本文件。)如果用户忘记了包含命令行参数,则程序应退出并显示相应的错误消息。

否则,您的程序应打印出:

  • 文件中的行数
  • 文件中的字符数
  • 文件中的元音数(出于此分配目的,将“ y”视为辅音。)
  • 文件中辅音的数量
  • 文件中小写字母的数量
  • 文件中大写字母的数量

我很茫然。 我该怎么做? 就像我很确定,有命令可以做到这一点,但我不知道它们是什么。 谢谢你的帮助 :)

编辑这是我的最终程序,它是完美的。 谢谢大家的帮助。 特别感谢Bentaye :)

import sys
def text(): 
    countV = 0 
    countC = 0 
    lines = 0 
    countU = 0
    countL = 0
    characters = 0 
    vowels = set("AEIOUaeiou") 
    cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ")    
    upper = set("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
    lower = set("abcdefghijklmnopqrstuvwxyz")

    with open(sys.argv[1]) as file:
        fileLines = file.readlines()

    for line in fileLines: 
        lines = lines + 1 
        characters = characters + len(line) 
        for char in line: 
            if char in vowels: 
                countV = countV + 1 
            elif char in cons: 
                countC = countC + 1
        for char in line:
            if char in upper:
                countU = countU + 1
            elif char in lower:
                countL = countL + 1
    print("Lines: " + str(lines)) 
    print("Characters: " + str(characters)) 
    print("Vowels: " + str(countV)) 
    print("Consonants: " + str(countC))
    print("Lowercase: " + str(countL))
    print("Uppercase: " + str(countU))
text()

这可以解决您的问题,现在可以在大/小写情况下使用

  • 使用sys.argv[0]捕获参数(您需要导入sys
  • 然后使用file.readlines()获取行数组(作为Strings)

import sys

countV = 0 
countC = 0 
lines = 0 
characters = 0 
vowels = set("AEIOUaeiou") 
cons = set("bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ") 

with open(sys.argv[0]) as file:
    fileLines = file.readlines()

    for line in fileLines: 
        lines = lines + 1 
        characters = characters + len(line) 
        for char in line: 
            if char in vowels: 
                countV = countV + 1 
            elif char in cons: 
                countC = countC + 1

    print("Lines: " + str(lines)) 
    print("Characters: " + str(characters)) 
    print (countV) 
    print (countC)

你这样叫

python test.py yourFile.txt 

完整答案供参考

import sys

vowels = "aeiou"
cons = "bcdfghjklmnpqrstvwxyz"

with open(sys.argv[0]) as file:
    fileLines = file.readlines()

    countVowels = 0 
    countConsonants = 0 
    countUpperCase = 0 
    countLowerCase = 0 
    countLines = 0 
    countCharacters = 0 
    countNonLetters = 0 

    for line in fileLines: 
        countLines += 1 
        countCharacters = countCharacters + len(line) 
        for char in line: 
            if char.isalpha():
                if char.lower() in vowels: 
                    countVowels += 1 
                elif char.lower() in cons: 
                    countConsonants += 1

                if char.isupper():
                    countUpperCase += 1
                elif char.islower():
                    countLowerCase += 1

            else:
                countNonLetters += 1

    print("Lines: " + str(countLines)) 
    print("Characters: " + str(countCharacters)) 
    print("Vowels: " + str(countVowels)) 
    print("Consonants: " + str(countConsonants)) 
    print("Upper case: " + str(countUpperCase)) 
    print("Lower case: " + str(countLowerCase)) 
    print("Non letters: " + str(countNonLetters)) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM