Python:count函数不起作用

时间:2016-03-12 11:43:08

标签: python-2.7 if-statement for-loop count continue

我坚持使用Coursera Python课程进行练习,这就是问题:

“打开文件mbox-short.txt并逐行读取。当您找到以'From'开头的行时,如下所示: 来自stephen.marquard@uct.ac.za 2008年1月5日星期六09:14:16 您将使用split()解析From行并打印出行中的第二个单词(即发送消息的人的整个地址)。然后打印出最后的计数。 提示:确保不要包含以“From:”开头的行。 您可以在http://www.pythonlearn.com/code/mbox-short.txt

下载示例数据

这是我的代码:

fname = raw_input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
    words = line.split()
    if len(words) > 2 and words[0] == 'From':
        print words[1]
        count = count + 1
    else:
        continue        
print "There were", count, "lines in the file with From as the first word"`

输出应该是电子邮件列表及其总和,但它不起作用,我不知道原因:实际上输出是“文件中有0行,其中From作为第一个单词”

7 个答案:

答案 0 :(得分:1)

我使用了您的代码并从链接下载了该文件。我得到了这个输出:

文件中有27行,其中From作为第一个单词

您是否检查过是否在与代码文件相同的位置下载文件。

答案 1 :(得分:0)

fname = input("Enter file name: ")
counter = 0
fh = open(fname)

for line in fh :
    line = line.rstrip()
    if not line.startswith('From '): continue        
    words = line.split()
    print (words[1])
    counter +=1

print ("There were", counter, "lines in the file with From as the first word")

答案 2 :(得分:0)

fname = input("Enter file name: ")

fh = open(fname)
count = 0

for line in fh :

    if line.startswith('From '): # consider the lines which start from the word "From "

        y=line.split() # we split the line into words and store it in a list
        print(y[1])   # print the word present at index 1
        count=count+1  # increment the count variable

print("There were", count, "lines in the file with From as the first word")

如果有人遇到任何困难,如果您需要帮助,请随时与我联系,我已经写了所有评论。这是互联网上最简单的代码。希望你从我的回答中受益

答案 3 :(得分:0)

fname = input('Enter the file name:')
fh = open(fname)
count = 0
for line in fh:
    if line.startswith('From'):
        linesplit =line.split()
        print(linesplit[1])
        count = count +1

答案 4 :(得分:0)

fname = input(“输入文件名:”) 如果len(fname)<1:fname =“ mbox-short.txt”

fh =打开(fname) 计数= 0 为我在fh中:

i=i.rstrip()

if not i.startswith('From '): continue
word=i.split() 
count=count+1

print(word[1])

print(“有”,计数,“文件中第一个单词是From的行”)

答案 5 :(得分:0)

fname = input("Enter file name: ")
if len(fname) < 1 : fname = "mbox-short.txt"
fh = open(fname)
count = 0
for line in fh:
    if line.startswith('From'):  
        line=line.rstrip()
        lt=line.split()
        if len(lt)==2:
            print(lt[1])
            count=count+1
print("There were", count, "lines in the file with From as the first word")

答案 6 :(得分:0)

我的代码看起来像这样并且很有魅力:

fname = input("Enter file name: ")
if len(fname) < 1:
    fname = "mbox-short.txt"

fh = open(fname)
count = 0 #initialize the counter to 0 for the start
for line in fh: #iterate the document line by line
    words = line.split() #split the lines in words
    if not len(words) < 2 and words[0] == "From": #check for lines starting with "From" and if the line is longer than 2 positions
        print(words[1]) #print the words on position 1 from the list
        count += 1 # count
    else:
        continue
print("There were", count, "lines in the file with From as the first word")

这是查克博士课程中的一个很好的练习

还有一种方法。您可以将找到的单词存储在单独的空列表中,然后打印出列表的长度。它将提供相同的结果。

我测试的代码如下:

fname = input("Enter file name: ")
if len(fname) < 1:
    fname = "mbox-short.txt"

fh = open(fname)
newl = list()
for line in fh:
    words = line.split()
    if not len(words) < 2 and words[0] == 'From':
        newl.append(words[1])
    else:
        continue
print(*newl, sep = "\n")
print("There were", len(newl), "lines in the file with From as the first word")

我也用它通过了练习。享受并保持良好的工作。尽管我一直讨厌编程,但 Python 对我来说非常有趣。