2017-10-18 190 views
0

我想編寫的代碼,將在命令行和 打印出來接受一個文件名以下屬性時,「詮釋」不是可迭代:蟒蛇參數使用在

  • 字符數
  • 數量的單詞
  • 數的「該」
  • 數的「一/一個」

我不斷收到錯誤消息

「參數類型的 '詮釋' 不是可迭代的」

爲線if 'the' in words:

我該如何解決這個問題?

import sys 
import string 

file_name=sys.argv[0] 

char= words = lines = theCount = aCount= 0 

with open(file_name,'r') as in_file: 
    for line in in_file: 
     lines +=1 
     words +=len(line.split()) 
     char +=len(line) 
     if 'the' in words: 
      theCount +=1 
     if 'a' in words: 
      a +=1 
     if 'an' in words: 
      a +=1 

print("Filename:", file_name) 
print("Number of lines:", lines) 
print("Number of characters:", char) 
print("Number of 'the'", theCount) 
print("Number of a/an:", aCount) 
+0

參見[這](https://docs.python.org/3/library/ collections.html#counter-objects)來自官方文檔的'計數器'配方,它顯示瞭如何做你想要的大部分。 –

回答

0

如果你正在試圖收集實際的話,而不僅僅是他們的計數,那麼也許你需要初始化話空單:

words = [] 

,改變

words += len(line.split()) 

words += line.split() 
0

有你的代碼的一些錯誤,在此剪斷閱讀註釋:

import sys 
#import string #not sure if this is needed 

file_name=sys.argv[0] 

char= words = lines = theCount = aCount= 0 


with open(file_name,'r') as in_file: 
    for line in in_file: 
     lines +=1 
     x = line.split() #use a variable to hold the split words 
         #so that you can search in it 
     words +=len(x) 
     char +=len(line) 
     if 'the' in x: #your original code was using "words" variable 
         #that holds the "number of words" in the line, 
         #therefore ints are not iterable 
      theCount +=1 
     if 'a' in x: 
      aCount +=1 #your original code using "a" variable 
         #which did not initialized, 
         #you have initialized "aCount" variable 
     if 'an' in x: 
      aCount +=1 #same as above 

print("Filename:", file_name) 
print("Number of lines:", lines) 
print("Number of characters:", char) 
print("Number of 'the'", theCount) 
print("Number of a/an:", aCount) 

https://repl.it/Mnwz/0

0

主要錯誤是,你正在尋找串inint。 您的變量words不是當前行中單詞的數組,它是所有行中累積的單詞數。第一次迭代之後,它將成爲第一行的單詞數量。

所以如果words = 3使用'the' in words是錯誤的。您應該保留當前行上的單詞列表以及所有單詞的增量計數。所以請撥打清單words和計數wordcount

words = line.split() 
wordcount += len(words) # set to 0 before loop 

現在'the' in words的作品。現在有另一個問題。如果一行中有多個單詞的實例,則只計算一個單詞。而是使用

theCount += words.count('the') 

把很多the的有上線,如何爲'a''an'這樣做。這不區分大小寫。因此,最好在分割之前將整條線路轉換爲較低的線路。

words = line.lower().split() 

您在開始時有其他錯誤。

file_name = sys.argv[0] 

argv[0]是正在執行的東西(即您的腳本名稱)的名稱。所以你會解析你自己的python腳本來計算它中的單詞。我不認爲你打算這麼做?如果你想利用命令行參數,它們從指數開始1

完整的腳本:

import sys 

file_name = sys.argv[1] 

chars = 0 
wordcount = 0 
lines = 0 
theCount = 0 
aCount= 0 

with open(file_name,'r') as in_file: 

    for line in in_file: 
     lines += 1 
     words = line.lower().split() 
     wordcount += len(words) 
     chars += len(line) 

     theCount += words.count('the') 
     aCount += words.count('a') 
     aCount += words.count('an') 


print("Filename:", file_name) 
print("Number of lines:", lines) 
print("Number of characters:", chars) 
print("Number of 'the'", theCount) 
print("Number of a/an:", aCount)