473,608 Members | 2,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with split() and rstrip()

4 New Member
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.marquar d@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.marquar d@uct.ac.za
louis@media.ber keley.edu
zqian@umich.edu
rjlowe@iupui.ed u
zqian@umich.edu
rjlowe@iupui.ed u
cwen@iupui.edu
cwen@iupui.edu
gsilver@umich.e du
gsilver@umich.e du
zqian@umich.edu
gsilver@umich.e du
wagnermr@iupui. edu
zqian@umich.edu
antranig@caret. cam.ac.uk
gopal.ramasammy cook@gmail.com
david.horwitz@u ct.ac.za
david.horwitz@u ct.ac.za
david.horwitz@u ct.ac.za
david.horwitz@u ct.ac.za
stephen.marquar d@uct.ac.za
louis@media.ber keley.edu
louis@media.ber keley.edu
ray@media.berke ley.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('Fro m') and line.rstrip(' SatFriThuJn0123 456789:')
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.marquar d@uct.ac.za Sat Jan 5 09:14:16 2008 ← Mismatch

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

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

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

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

From rjlowe@iupui.ed u 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.e du Fri Jan 4 11:12:37 2008

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

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

From gsilver@umich.e du 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.ramasammy cook@gmail.com Fri Jan 4 09:05:31 2008

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

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

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

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

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

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

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

From ray@media.berke ley.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 9723
dev7060
644 Recognized Expert Contributor
From stephen.marquar d@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 New Member
The rstrip() method removes any trailing characters at the end a string, space is the default trailing character*to*re move.

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*appreciat ed.
Jan 28 '23 #3
Arushi
7 New Member
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
20663
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 >>> mystr = "test.txt" >>> mystr = string.rstrip(mystr, ".txt")
11
1242
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 from the form in my webpage. Now I have to do some slicing to get the name of the form elements (in this case, checkboxes), to resemble the values in the inventory file. Here's my python part: #!/usr/local/bin/python
6
2339
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, PyObject *args){ int n, input; if (!PyArg_ParseTuple(args, "i", &input))
8
1809
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 my requirement.
2
1888
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
1967
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 split up due to those random quotations postgresql returns to me. Please help me with this, Thanks, Sia
5
1400
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 0:00:00 4,3,"OA of lower leg",9/12/2003 0:00:00 5,4,"Cholera NOS",9/12/2003 0:00:00 6,4,"Open wound of ear NEC*",9/12/2003 0:00:00 7,4,"Migraine with aura",9/12/2003 0:00:00 8,6,"HTN ",10/15/2003 0:00:00
2
1583
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 python 2.5.1, this same code fails to work on an ubuntu server with python 2.4.3 I was wondering if there is a problem with .rstrip() in that python version?
8
930
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 later). I have done this many many times. Suddenly, it does not work any more: The value frq is different from the value that key has in the file 'my_frqlist.txt'.
0
874
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 names to all your variable names, to remove unused variables (like n), to add blank likes here and there to separate logically separated parts of your program, or even better to split it into functions. You can remove some intermediate function,...
0
8000
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8495
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8470
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8330
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6815
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6011
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5475
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4023
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.