Videos
Hello, I am currently learning to python with the book "Python for everybody". I have just done this excercise:
Exercise 5: Minimalist Email Client.MBOX (mail box) is a popular file format to store and share a collection of emails. This was used by early email servers and desktop apps. Without getting into too many details, MBOX is a text file, whichstores emails consecutively. Emails are separated by a special line which starts with From (notice the space). Importantly, lines starting with From: (notice the colon) describes the email itself and does not act as a separator. Imagine you wrote a minimalist email app, that lists the email of the senders in the user’s Inbox and counts the number of emails. Write a program to read through the mail box data and when you find line that starts with “From”, you will split the line into words using the split function. We are interested in who sent the message, which is the second word on the From line.
From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008
You will parse the From line and print out the second word for each From line, then you will also count the number of From (not From:) lines and print out a count at the end. This is a good sample outputwith a few lines removed
:python fromcount.py
Enter a file name: mbox-short.txt
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
[...some output removed...
*]*ray@media.berkeley.edu
cwen@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
There were 27 lines in the file with From as the first word
This is my code:
fhand = open(input('Please enter a file name: '))count = 0for line in fhand :if line.startswith('From ') :count += 1line.rstrip()print(line.split(' ')[1])print('There were', count, 'lines in the file with From as the first word')
I get a correct output, though, this excercise is at the end of a chapter Lists and, as far as I understand, my code does not create any(?). Could you suggest how to rewrite it using a list (for me to better understand the list/string methods).