본문 바로가기

개발/Python

py4e 8.5 From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008

8.5 Open the file mbox-short.txt and read it line by line. When you find a line that starts with 'From ' like the following line:From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008 You will parse the From line using split() and print out the second word in the line (i.e. the entire address of the person who sent the message). Then print out a count at the end.

Hint: make sure not to include the lines that start with 'From:'. Also look at the last line of the sample output to see how to print the count.

 

 

저번 글에는 과제 수행에서 막히면 다시 복습 겸 수업을 복습하라고 했는데 더 좋은 방법이 있었다.

수업에서 다룬 코드를 다 따라 치며 주석을 달고 저장하는 거지... 

 

>>> it gets much easier to do assignments

if you note all the codes in matarials, especially if you are not native English speaker. 

 

it's great to study, after all. 

 

fname = input("Enter file name: ")
fh = open("mbox-short.txt")
count = 0
for line in fh:
    line = line.rstrip()
    if line.endswith("2008") and '@' in line:
        line = line.split()
        mail = line[1]
        print(mail)
        count += 1
    else:
        continue        
        
print("There were", count, "lines in the file with From as the first word")

Here's my code from class note.

I'm very glad that I didn't google anything to do this assignments.