Python—如何在文件中打印数字、句点和逗号的数量 def showCounts(文件名): 行数=0 字数=0 numCount=0 通信量=0 点计数=0 将open(fileName,'r')作为f: 对于f中的行: words=line.split() 行数+=1 字数+=len(字) 用文字表示: #####text=word.translate(字符串.标点符号) 排除=设置(字符串.标点符号) text=“” text=''.join(如果ch不在exclude中,则ch代表文本中的ch) 尝试: 如果int(text)>=0或int(text)
基本上它会显示行数和单词数,但我不能让它显示数字、逗号和点的数量。我让它读取一个用户输入的文件,然后显示行和字的数量,但出于某种原因,它显示数字逗号和点为0。我把给我带来麻烦的部分注释掉了。如果我删除逗号,那么我只会得到一个错误。感谢大家提供的标点符号,为什么不:Python—如何在文件中打印数字、句点和逗号的数量 def showCounts(文件名): 行数=0 字数=0 numCount=0 通信量=0 点计数=0 将open(fileName,'r')作为f: 对于f中的行: words=line.split() 行数+=1 字数+=len(字) 用文字表示: #####text=word.translate(字符串.标点符号) 排除=设置(字符串.标点符号) text=“” text=''.join(如果ch不在exclude中,则ch代表文本中的ch) 尝试: 如果int(text)>=0或int(text),python,Python,基本上它会显示行数和单词数,但我不能让它显示数字、逗号和点的数量。我让它读取一个用户输入的文件,然后显示行和字的数量,但出于某种原因,它显示数字逗号和点为0。我把给我带来麻烦的部分注释掉了。如果我删除逗号,那么我只会得到一个错误。感谢大家提供的标点符号,为什么不: def showCounts(fileName): lineCount = 0 wordCount = 0 numCount = 0 comCount = 0 dotCount = 0 with open(fileName, 'r'
def showCounts(fileName):
lineCount = 0
wordCount = 0
numCount = 0
comCount = 0
dotCount = 0
with open(fileName, 'r') as f:
for line in f:
words = line.split()
lineCount += 1
wordCount += len(words)
for word in words:
# ###text = word.translate(string.punctuation)
exclude = set(string.punctuation)
text = ""
text = ''.join(ch for ch in text if ch not in exclude)
try:
if int(text) >= 0 or int(text) < 0:
numCount += 1
# elif text == ",":
# comCount += 1
# elif text == ".":
# dotCount += 1
except ValueError:
pass
print("Line count: " + str(lineCount))
print("Word count: " + str(wordCount))
print("Number count: " + str(numCount))
print("Comma count: " + str(comCount))
print("Dot count: " + str(dotCount) + "\n")
此代码在每行中的每个字符上循环,并向其变量添加1:
def showCounts(fileName):
...
...
with open(fileName, 'r') as fl:
f = fl.read()
comCount = f.count(',')
dotCount = f.count('.')
测试它:
Hello, my name is B.o.b. I like biking, swimming, and running.
I am 125 years old, and I was 124 years old 1 year ago.
Regards,
B.o.b
test.txt
:
numCount = 0
dotCount = 0
commaCount = 0
lineCount = 0
wordCount = 0
fileName = 'test.txt'
with open(fileName, 'r') as f:
for line in f:
wordCount+=len(line.split())
lineCount+=1
for char in line:
if char.isdigit() == True:
numCount+=1
elif char == '.':
dotCount+=1
elif char == ',':
commaCount+=1
print("Number count: " + str(numCount))
print("Comma count: " + str(commaCount))
print("Dot count: " + str(dotCount))
print("Line count: " + str(lineCount))
print("Word count: " + str(wordCount))
跑步:
Hello, my name is B.o.b. I like biking, swimming, and running.
I am 125 years old, and I was 124 years old 1 year ago.
Regards,
B.o.b
这里一切都有意义,除了lineCount
这是6
的原因是因为换行。在我的编辑器(nano)中,默认情况下,它会在任何文件的末尾添加一个换行符。因此,只需将文本文件想象为:
bash-3.2$ python count.py
Number count: 7
Comma count: 5
Dot count: 7
Line count: 6
Word count: 27
bash-3.2$
希望这有帮助 您可以使用
计数器
类来处理它,您可以:
>>> x = open('test.txt').read()
>>> x
'Hello, my name is B.o.b. I like biking, swimming, and running.\n\nI am 125 years old, and I was 124 years old 1 year ago.\n\nRegards,\nB.o.b \n'
>>> x.count('\n')
6
>>>
“那么剩下的就保持现在的样子吧?”我想是的。你为什么不试试看,然后告诉我。我发现了这个错误。回溯(最后一次调用):文件“assn.py”,第113行,在showCounts(_文件)文件“assn.py”,第61行,在showCounts words=line.split()name中错误:全局名称“line”未定义。古怪的它的数字、点和逗号都是正确的,但它为行和词提供的数字远远高于它们的实际值……对不起,我打错了。逗号和点的数字很好-非常感谢!出于某种原因,我有9行,但它说我有98行,我可能有20个单词,但它说我有304行…这很有帮助,但只是给了我更多的问题-我不知道为什么它给出了错误的行数和单词数?让我加上字数和行数这表示行没有定义?@TommyConnor它对我有用。是否确实要在上下文管理器中分配给
行
,而不是行
?