473,416 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Problem with split() and rstrip()

4 2Bits
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?
Jan 20 '23 #1
3 9705
dev7060
636 Expert 512MB
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).
Expand|Select|Wrap|Line Numbers
  1. x = line.split(' ')[1]
‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎‎ ‎
Jan 24 '23 #2
pritikumari
23 16bit
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.
Jan 28 '23 #3
Arushi
7 Nibble
The correct code to achieve the desired output should be:

Expand|Select|Wrap|Line Numbers
  1. fname = input("Enter file name: ")
  2. if len(fname) < 1:
  3. fname = "mbox-short.txt"
  4.  
  5. fh = open(fname)
  6. count = 0
  7. for line in fh:
  8. if line.startswith('From:'):
  9. pass
  10. elif line.startswith('From'):
  11. words = line.split()
  12. email = words[1]
  13. print(email)
  14.  
  15. count = count + 1
  16.  
  17. print("There were", count, "lines in the file with From as the first word")
Jan 30 '23 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

8
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 >>>...
11
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...
6
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,...
8
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...
2
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.
12
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...
5
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...
2
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...
8
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
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...
0
Oralloy
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,...
0
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...
0
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.