我正在研究一个我被分配的程序,它只是获取十四行诗的单词和行数。这里的第一部分代码是有效的,也是我教授正在寻找的正确输出,即使它包含十四行诗的前两行。
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()
但是,分配的下一部分是使用第二个函数“countNum”获得相同的结果,参数为“line”。所以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.
答案 0 :(得分:1)
您的countNum
正在打印结果而不是返回结果:
def countNum(line):
return len(line.split())
此外,您的密切方法需要在()
之后。它们实际上并没有执行:
infile.close
outfile.close
到
infile.close()
outfile.close()
答案 1 :(得分:0)
def count(line):
return len(line.split())
def main():
infile = open(input("File to analyse: "), "r")
file = infile.read().splitlines()
infile.close()
lineCount = len(file)
wordTotal = 0
for line in file:
words = count(line)
wordTotal += words
print("Lines:", lineCount)
print("Words:", wordTotal)
main()
答案 2 :(得分:0)
您可以使用len
来获取列表的长度。行列表由f.readlines()
给出,一行中的单词列表由line.split()
给出。我们可以使用内置sum
来对列表求和,并且是超级pythonic
https://docs.python.org/3/library/functions.html#sum
您应该使用with
关键字自动关闭Python。 https://docs.python.org/3/tutorial/inputoutput.html
所以我们有:
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)
有关列表推导的信息,请参阅https://docs.python.org/3.6/tutorial/datastructures.html#list-comprehensions