Python 2.7 Python:count函数不起作用

Python 2.7 Python:count函数不起作用,python-2.7,if-statement,for-loop,count,continue,Python 2.7,If Statement,For Loop,Count,Continue,我被困在Coursera Python课程的一个练习中,问题是: “打开文件mbox-short.txt并逐行读取。当您发现一行以“From”开头时,如以下行所示: 斯蒂芬。marquard@uct.ac.za2008年1月5日星期六09:14:16 您将使用split()解析From行,并打印出该行中的第二个单词(即发送消息的人的整个地址),然后在末尾打印一个计数。 提示:确保不包括以“From:”开头的行。 您可以在以下网址下载示例数据“ 这是我的密码: fname = raw_input(

我被困在Coursera Python课程的一个练习中,问题是:

“打开文件mbox-short.txt并逐行读取。当您发现一行以“From”开头时,如以下行所示: 斯蒂芬。marquard@uct.ac.za2008年1月5日星期六09:14:16 您将使用split()解析From行,并打印出该行中的第二个单词(即发送消息的人的整个地址),然后在末尾打印一个计数。 提示:确保不包括以“From:”开头的行。 您可以在以下网址下载示例数据“

这是我的密码:

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"`
fname=raw\u输入(“输入文件名:”)
如果len(fname)<1:fname=“mbox short.txt”
fh=打开(fname)
计数=0
对于fh中的线路:
words=line.split()
如果len(单词)>2且单词[0]=“From”:
印刷文字[1]
计数=计数+1
其他:
继续
打印“有”,计数,“文件中以From作为第一个单词的行”`

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

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

文件中有27行,第一个字是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: ")
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 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
如果len(fname)<1:fname=“mbox short.txt”

fh=打开(fname) 计数=0 对于fh中的i:

i=i.rstrip()

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

print(word[1])
打印(“文件中有”,计数,“行,第一个字是From”)

fname=input(“输入文件名:”)
如果len(fname)<1:fname=“mbox short.txt”
fh=打开(fname)
计数=0
对于fh中的线路:
如果line.startswith('From'):
line=line.rstrip()
lt=line.split()
如果len(lt)==2:
打印(lt[1])
计数=计数+1
打印(“有”,计数,“文件中第一个字是From的行”)

我的代码看起来像这样,可以作为一种魅力:

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(“输入文件名:”)
如果len(fname)<1:
fname=“mbox short.txt”
fh=打开(fname)
计数=0#启动时将计数器初始化为0
对于fh中的行:#逐行迭代文档
words=line.split()#将行拆分为words
如果不是len(words)<2且words[0]=“From”:#检查以“From”开头的行以及行是否超过2个位置
打印(单词[1])#从列表中打印位置1上的单词
计数+=1#计数
其他:
持续
打印(“有”,计数,“文件中第一个字是From的行”)
这是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")
fname=input(“输入文件名:”)
如果len(fname)<1:
fname=“mbox short.txt”
fh=打开(fname)
newl=list()
对于fh中的线路:
words=line.split()
如果不是len(单词)<2,单词[0]=“From”:
newl.append(单词[1])
其他:
持续
打印(*newl,sep=“\n”)
打印(“文件中有”,len(newl),“第一个字是From的行”)

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

你检查过for循环的缩进了吗?你好像忘了缩进循环下面的行了。谢谢你Selçuk,我已经纠正了缩进,但它不起作用。。我认为有两个问题:我不知道如何写打印声明来打印电子邮件列表,而且计数不正常,很可能文件无法打开或采用其他格式。因为你的循环看起来很好。尝试添加“打印文字”以查看行是否正在打印,并且是否符合您期望的格式。这真的很奇怪,我将您的代码复制到了一个py文件中。从您提供的链接中复制了mbox-short.txt中的数据。对我来说似乎很好。我得到了以下输出,斯蒂芬。marquard@uct.ac.za louis@media.berkeley.edu文件中有两行,第一个字是From。重点是输出应该是27封电子邮件,字符串“文件中有27行,第一个字是From”。顺便说一句,这是你应该编辑文章的练习,包括对代码实现的功能的解释。虽然这段代码可能会解决这个问题,但如何以及为什么解决这个问题将真正有助于提高文章的质量,并可能导致更多的投票。请记住,你是在将来回答读者的问题,而不仅仅是现在提问的人。请在回答中添加解释,并说明适用的限制和假设。
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")