2017-10-18 75 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)