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
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?
3 9705
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).
The rstrip() method removes any trailing characters at the end a string, space is the default trailing character*to*remove.
I have looked at a lot of other people's questions/answers about split and strip, and I think that maybe some of my problem is that my initial code splits the text by line, thus giving me lists to work with, when in actuality, each of my lines is still a string that I need to break up and NOT a list, but because of python syntax I have to work with the string as if it were a list. Please, any advice you can give me that would help me understand what my problem is and how to fix it would be*so*appreciated.
The correct code to achieve the desired output should be: - 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'):
-
words = line.split()
-
email = words[1]
-
print(email)
-
-
count = count + 1
-
-
print("There were", count, "lines in the file with From as the first word")
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hank |
last post by:
Hi,
I have this problem with string.rstrip
Python 2.2.3 (#42, May 30 2003, 18:12:08) on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import string
>>>...
|
by: Chris |
last post by:
Hi,
I'm new to python, and I'm trying to write a small python script for a
webpage. The script opens up a file called inventory, reads the
contents, and checks the contents against the data...
|
by: ajikoe |
last post by:
Hello I tried to combine c++ and python together.
So I follow from this website:
http://kortis.to/radix/python_ext/
I have this code:
# prmodule.c
static PyObject *pr_isprime(PyObject *self,...
|
by: =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z |
last post by:
How could I format the float number like this: (keep 2 digit
precision)
1.002 =1
1.12 =1.12
1.00 =1
1.567 =1.57
2324.012 =2324.01
I can not find any Formatting Operations is able to meet...
|
by: kyo guan |
last post by:
Hi :
Please look at this code:
'ex' <----- it should be 'exe', why?
but this is a right answer:
'120' <------ this is a right value.
|
by: Siah |
last post by:
Hi,
I need to convert the string: '(a, b, "c", d, "e")' into the following
list . Much like a csv reader does. I usually
use the split function, but this mini-monster wouldn't properly get...
|
by: barronmo |
last post by:
I'm having difficulty getting the following code to work. All I want
to do is remove the '0:00:00' from the end of each line. Here is part
of the original file:
3,3,"Dyspepsia NOS",9/12/2003...
|
by: dirkheld |
last post by:
Hi,
I wrote some python code that retrieves urls from a txt file. In this
code I use .rstrip() for removing the '\n' at the end of every url.
While this code works on my mac (leopard) with...
|
by: Simon Strobl |
last post by:
Hello,
the idea of the following program is to parse a frequency list of the
form FREQUENCY|WORD, to store the frequency of a word in a dictionary
(and to do some things with this information...
|
by: bearophileHUGS |
last post by:
Subhabrata, it's very difficult for me to understand what your short
program has to do, or what you say. I think that formatting and code
style are important.
So I suggest you to give meaningful...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |