2014-05-04 53 views
0
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") 

基本上,它會顯示的行數和文字的數量,但我不能得到它顯示的數量數字,逗號和點。我讀了一個文件,用戶輸入,然後顯示大量的行和單詞,但由於某種原因,它表示數字逗號和點數爲0。我評論了它給我帶來麻煩的部分。如果我刪除逗號,那麼我只是得到一個錯誤。謝謝你們Python的 - 如何打印數字,句號和逗號的數量在文件

回答

0

該代碼將遍歷每行每一個字符,並增加了1到它的變量:

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

測試出來:

test.txt

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 

運行:

bash-3.2$ python count.py 
Number count: 7 
Comma count: 5 
Dot count: 7 
Line count: 6 
Word count: 27 
bash-3.2$ 

一切都使得這裏的意義,除了lineCount之所以這是6是因爲新行的。在我的編輯器(nano)中,默認情況下它會在任何文件的末尾添加換行符。所以想象一下這個文本文件是這樣的:

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

希望這有助於!

+0

嗯。奇。它的數字點和逗號是正確的,但它提供的行數和單詞數量遠遠高於他們實際上的數字...... – TommyConnor

+0

Are you sure?它在我上面的演示中得到了正確的... –

+0

對不起,我誤打錯了。數字逗號和點是好的 - 非常感謝!由於某種原因,我有9行,但它說我有98,我可能有20個字,但它說我有304 ... – TommyConnor

0

對於標點符號,爲什麼不只是做:

def showCounts(fileName): 
    ... 
    ... 
    with open(fileName, 'r') as fl: 
     f = fl.read() 

    comCount = f.count(',') 
    dotCount = f.count('.') 
+0

,然後自顧自地休息,因爲它是現在? – TommyConnor

+0

@TommyConnor,我相信。你爲什麼不試一試,看看它如何,然後讓我知道。 – sshashank124

+0

我得到這個錯誤。 回溯(最近最後調用): 文件 「assn.py」,線路113,在 showCounts(THE_FILE) 文件 「assn.py」,第61行,在showCounts 詞語= line.split() NameError:全局名稱'行'未定義 – TommyConnor

0

您可以使用Counter類照顧它你:

from collections import Counter 

with open(fileName, 'r') as f: 
    data = f.read().strip() 
    lines = len(data.split('\n')) 
    words = len(data.split()) 
    counts = Counter(data) 
    numbers = sum(v for (k,v) in counts.items() if k.isdigit()) 

print("Line count: {}".format(lines)) 
print("Word count: {}".format(words)) 
print("Number count: {}".format(numbers)) 
print("Comma count: {}".format(counts[','])) 
print("Dot count: {}".format(counts['.'])) 
+0

這說行不定義? – TommyConnor

+0

@TommyConnor它適合我。你確定你是在上下文管理器中分配給'lines'而不是'line'嗎? –

相關問題