Python中使用多个函数的字计数器

Python中使用多个函数的字计数器,python,Python,我目前正在做一个我被分配的程序,这个程序只是得到一首十四行诗的字数和行数。这里的第一段代码是有效的,是我的教授想要的正确的输出,尽管它包含了十四行诗的前两行 import string def main(): ifName = input("What file would you like to analyze? ") ofName = input("What file should the results be written to? ") infile = o

我目前正在做一个我被分配的程序,这个程序只是得到一首十四行诗的字数和行数。这里的第一段代码是有效的,是我的教授想要的正确的输出,尽管它包含了十四行诗的前两行

import string


def main():

    ifName = input("What file would you like to analyze? ")
    ofName = input("What file should the results be written to? ")

    infile = open(ifName, "r")
    outfile = open(ofName, "w")

    lineCount = 0
    wordCount = 0

    for line in infile:
        lineCount +=1
        wordLine = line.split()
        L = len(wordLine)
        wordCount += L


    print("The file", ifName, "had:", file= outfile)
    print("words =", wordCount, file= outfile)
    print("lines =", lineCount, file= outfile)
    print("The results have been printed to:", outfile)

    infile.close
    outfile.close

main()
但是,赋值的下一部分是使用参数为“line”的第二个函数“countNum”获得相同的结果。所以countNum(行)

这是我一直在乱搞的代码,看看我是否能让它工作

import string


def countNum(line):
    wordCount = 0
    wordLine = line.split()
    L = len(wordLine)
    wordCount +=L
    print(wordCount)

def main():

    ifName = input("What file would you like to analyze? ")
    ofName = input("What file should the results be written to? ")

    infile = open(ifName, "r")
    outfile = open(ofName, "w")

    lineCount = 0
    wordCount = 0

    for line in infile:
        lineCount +=1
        wordTotal += countNum(line)
        ##wordLine = line.split()
        ##L = len(wordLine)
        ##wordCount += L


  ##  print("The file", ifName, "had:", file= outfile)
  ##  print("words =", wordCount, file= outfile)
  ##  print("lines =", lineCount, file= outfile)
  ##  print("The results have been printed to:", outfile)

    infile.close
    outfile.close

main()
如果您想知道,这是sonnet.txt文件:

Shakespeare’s Sonnet 18 Shall I compare thee to a summer's day? Thou art more lovely and more temperate: Rough winds do shake the darling buds of May, And summer's lease hath all too short a date: Sometime too hot the eye of heaven shines, And often is his gold complexion dimm'd; And every fair from fair sometime declines, By chance or nature's changing course untrimm'd; But thy eternal summer shall not fade Nor lose possession of that fair thou owest; Nor shall Death brag thou wander'st in his shade, When in eternal lines to time thou growest: So long as men can breathe or eyes can see, So long lives this, and this gives life to thee. 莎士比亚十四行诗18 我能把你比作夏日吗? 你更可爱,更温和: 狂风吹拂着五月可爱的花蕾, 夏天的租约日期太短了: 有时候,天眼的光芒过于炽热, 他金色的肤色常常黯淡; 每一个集市都会有衰落的时候, 由于偶然或自然的变化过程而未受影响; 而你如仲夏繁茂不凋谢 也不要失去你所拥有的美丽; 死亡也不会吹嘘你在他的阴影下徘徊, 当你在永恒的诗句中成长时: 只要人能呼吸,眼睛能看见, 这生命如此长久,这给了你生命。
您的
countNum
正在打印结果,而不是返回结果:

def countNum(line):
    return len(line.split())
此外,您的close方法后面需要有
()
。他们实际上并没有执行:

infile.close
outfile.close
infile.close()
outfile.close()


您可以使用
len
获取列表的长度。行列表由
f.readlines()
给出,行中的单词列表由
line.split()
给出。我们可以使用内置的
sum
来对一个列表进行求和,并且是超级pythonic

您应该通过使用
with
关键字来使用Python的自动关闭

所以我们有:

with open(infilename, "r") as infile, open(outfilename, "w") as outfile:
    lines = infile.readlines()
    linecount = len(lines)
    wordcount = sum([len(line.split()) for line in lines])
    print(linecount)
    print(wordcount)

有关列表理解的信息,请参见

是否要计算一行中每个单词的出现次数?只需从函数返回wordCount即可return wordCount'顺便说一句,你并没有真正关闭你的文件。
with open(infilename, "r") as infile, open(outfilename, "w") as outfile:
    lines = infile.readlines()
    linecount = len(lines)
    wordcount = sum([len(line.split()) for line in lines])
    print(linecount)
    print(wordcount)