【问题标题】:Iterable error in word count Python 3.3 program字数 Python 3.3 程序中的可迭代错误
【发布时间】:2012-10-19 06:31:25
【问题描述】:

我正在尝试完成一个简单的字数统计程序,该程序会跟踪连接文件中的字数、字符数和行数。

   # This program counts the number of lines, words, and characters in a file, entered by the user.
   # The file is test text from a standard lorem ipsum generator.
   import string
   def wc():
      # Sets the count of normal lines, words, and characters to 0 for proper iterative operation.
        lines = 0
        words = 0
        chars = 0
        print("This program will count the number of lines, words, and characters in a file.")
       # Stores a variable as a string for more graceful coding and no errors experienced previously. 
       filename =("test.txt")
       # Opens file and stores it as new variable, and loops through each line once the connection   with file is made.
       with open(filename, 'r') as fileObject:
             for l in fileObject:
                # Splits text file into each individual word for word count.
                words = l.split()

                lines += 1
                words += len(words)
                chars += len(l)
        print("Lines:", lines)
        print("Words:", words)
        print("Characters:", chars)

    wc()

    while 1:
        pass

现在,如果一切顺利,它应该会打印文件中的行数、字母和单词的总数,但我得到的只是这条消息:

"单词 += len(单词) TypeError:“int”对象不可迭代 "

怎么了?

已解决!新代码:

    # This program counts the number of lines, words, and characters in a file, entered by the user.
    # The file is test text from a standard lorem ipsum generator.
    import string
    def wc():
        # Sets the count of normal lines, words, and characters to 0 for proper iterative operation.
        lines = 0
        words = 0
        chars = 0
        print("This program will count the number of lines, words, and characters in a file.")
        # Stores a variable as a string for more graceful coding and no errors experienced previously. 
        filename =("test.txt")
        # Opens file and stores it as new variable, and loops through each line once the connection with file is made.
        with open(filename, 'r') as fileObject:
            for l in fileObject:
                # Splits text file into each individual word for word count.
                wordsFind = l.split()

                lines += 1
                words += len(wordsFind)
                chars += len(l)
                 print("Lines:", lines)
                 print("Words:", words)
                 print("Characters:", chars)

    wc()

    while 1:
        pass

【问题讨论】:

    标签: python file file-io python-3.x word-count


    【解决方案1】:

    您似乎正在使用变量名称 words 进行计数,并且还使用了 l.split() 的结果。您需要通过为它们使用不同的变量名称来区分它们。

    【讨论】:

    • +1。起初,这看起来像是习惯于不同语言的人的范围混淆。但实际上,他试图在同一行中使用它们,words += len(words)。无论您如何解释它,您要么尝试 len 一个 int,要么将一个 int 附加到一个字符串列表......
    • 现在工作。添加了我的最终工作代码以供证明。 @Andew 谢谢!我主要习惯于 Java,因此对于我作为一个相对较新的 Python 3.3 用户的错误表示歉意。
    猜你喜欢
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    • 2017-06-22
    • 2014-10-09
    • 2017-06-29
    相关资源
    最近更新 更多