我被 Coursera Python 课程的练习困住了,这就是问题:
“打开文件 mbox-short.txt 并逐行读取它。当您找到以 'From ' 开头的行时,如下所示: 来自 [电子邮件受保护] 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 作为第一个单词”
我使用了您的代码并从链接下载了文件。我得到这个输出:
文件中有 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("请输入文件名:") if 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 对我来说非常有趣。
file_name = input('Enter a file name: ')
count = 0
try:
file_handle = open(file_name)
for line in file_handle:
words = line.rstrip().split()
if len(words) == 0:
continue
if words[0] == 'From':
count += 1
print(words[1])
print('There were', count, 'lines in the file with From as the first word')
except:
print('Invalid File')