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.
'개발 > Python' 카테고리의 다른 글
py4e 9.4 who has sent the greatest number of mail messages? (0) | 2020.11.29 |
---|---|
파이썬 무료 강의 추천 : 모두를 위한 파이썬 Python for everybody (0) | 2020.11.28 |
py4e exercise 7.2 (0) | 2020.11.28 |
py4e Assignments 3.3 (0) | 2020.11.20 |
class, function, object, instance, parameter, variable (0) | 2020.08.26 |