Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have the following assgnment:

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.

You can download the sample data at http://www.py4e.com/code3/mbox-short.txt

The output should be:

stephen.marquard@uct.ac.za
louis@media.berkeley.edu
zqian@umich.edu
rjlowe@iupui.edu
zqian@umich.edu
rjlowe@iupui.edu
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.edu
gsilver@umich.edu
zqian@umich.edu
gsilver@umich.edu
wagnermr@iupui.edu
zqian@umich.edu
antranig@caret.cam.ac.uk
gopal.ramasammycook@gmail.com
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
david.horwitz@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
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

What I have tried:

I tried it with the code below:

``````````
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:'):
        pass    
    elif line.startswith('From'):
        x = line.split('From') and line.rstrip(' SatFriThuJn0123456789:')
        print(x)
 
        count = count + 1
       
print("There were", count, "lines in the file with From as the first word")

```````````````

The output I'm getting is the following:


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

From louis@media.berkeley.edu Fri Jan 4 18:10:48 2008

From zqian@umich.edu Fri Jan 4 16:10:39 2008

From rjlowe@iupui.edu Fri Jan 4 15:46:24 2008

From zqian@umich.edu Fri Jan 4 15:03:18 2008

From rjlowe@iupui.edu Fri Jan 4 14:50:18 2008

From cwen@iupui.edu Fri Jan 4 11:37:30 2008

From cwen@iupui.edu Fri Jan 4 11:35:08 2008

From gsilver@umich.edu Fri Jan 4 11:12:37 2008

From gsilver@umich.edu Fri Jan 4 11:11:52 2008

From zqian@umich.edu Fri Jan 4 11:11:03 2008

From gsilver@umich.edu Fri Jan 4 11:10:22 2008

From wagnermr@iupui.edu Fri Jan 4 10:38:42 2008

From zqian@umich.edu Fri Jan 4 10:17:43 2008

From antranig@caret.cam.ac.uk Fri Jan 4 10:04:14 2008

From gopal.ramasammycook@gmail.com Fri Jan 4 09:05:31 2008

From david.horwitz@uct.ac.za Fri Jan 4 07:02:32 2008

From david.horwitz@uct.ac.za Fri Jan 4 06:08:27 2008

From david.horwitz@uct.ac.za Fri Jan 4 04:49:08 2008

From david.horwitz@uct.ac.za Fri Jan 4 04:33:44 2008

From stephen.marquard@uct.ac.za Fri Jan 4 04:07:34 2008

From louis@media.berkeley.edu Thu Jan 3 19:51:21 2008

From louis@media.berkeley.edu Thu Jan 3 17:18:23 2008

From ray@media.berkeley.edu Thu Jan 3 17:07:00 2008

From cwen@iupui.edu Thu Jan 3 16:34:40 2008

From cwen@iupui.edu Thu Jan 3 16:29:07 2008

From cwen@iupui.edu Thu Jan 3 16:23:48 2008

There were 27 lines in the file with From as the first word[


As you can see, the last line is correct (count)and the email addresses are at the right order....but I'm not being able to remove "From" at the beginning of the lines neither the dates at the end of the lines. Another thing is that the lines are skipped and they shoudn't.


Could someone help me with that task, please?
Posted
Updated 20-Jan-23 15:56pm
v3

1 solution

You seem to have a misunderstanding of what the arguments to split and rstrip() do. In both cases the argument supplies a set of characters that the method applies to the string. For example:
Python
>>> str='From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
>>> x = str.split('From')
>>> x
['', ' stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008']
>>> 

That's not what you've been asked for at all. On the other hand
Python
>>> str=
'From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008'
>>> x = str.split()
>>> x
['From', 'stephen.marquard@uct.ac.za', 'Sat', 'Jan', '5', '09:14:16', '2008']

So a simpler solution would be
Python
for line in fh:
   x = line.split()
   if x[0] == 'From':
       print(x[1])
       count += 1
 
Share this answer
 
Comments
Guilherme Romero 21-Jan-23 10:08am    
Thanks very much, it worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900