如何使用 JES 计算文件中的字符数(Python 学生)
How to count characters in a file using JES (Python for students)
如何让 python 读取文本文件和 return lines
、characters
、vowels
、consonants
的数量, lowercase letters
,以及 uppercase 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()
这解决了您的问题,您现在可以在 upper/lower 案例
的基础上进行构建
- 使用
sys.argv[0]
捕获参数(需要导入sys
)
- 然后使用
file.readlines()
获取行数组(作为字符串)
代码
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))
如何让 python 读取文本文件和 return lines
、characters
、vowels
、consonants
的数量, lowercase letters
,以及 uppercase 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()
这解决了您的问题,您现在可以在 upper/lower 案例
的基础上进行构建- 使用
sys.argv[0]
捕获参数(需要导入sys
) - 然后使用
file.readlines()
获取行数组(作为字符串)
代码
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))