473,287 Members | 1,395 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,287 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 9695
dev7060
626 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: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.