Python:计数功能不起作用

Python : count function does not work

我卡在了 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"`

输出应该是电子邮件列表和它们的总和,但它不起作用,我不知道为什么:实际上输出是 "There were 0 lines in the file with From as the first word"

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

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

你检查过你下载的文件是否和代码文件在同一个位置吗

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")
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")

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

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

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("There were", count, "文件中以 From 作为第一个单词的行")

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")

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

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")

Chuck 博士课程中的一个很好的练习

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

我测试的代码如下:

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 对我来说非常有趣,尽管我一直讨厌编程。