By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,272 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,272 IT Pros & Developers. It's quick & easy.

search a part in a string

P: 4
Completely newbie to scripting !! :
I would like to get the subject out of a mail message and put this in a seperate string:
mailMsg = mail.retr(i)[1]

example result of mailMsg is :

['Return-Path: <Louisette.Snoekx@123.com>', 'Delivered-To: 3-marc.jonkers@456.com', 'From: "louisette snoekx" <Louisette.Snoekx@123.com>', 'To: <marc.jonkers@456.com>', 'References: <20060919215507.cvi40klhf1q8884k@webmail.456.com>' , 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100', .. etc]

I would like the string subject(i) to contain in this example 'Gelezen: workshop 15/10 essen'
I suppose it must be something like

for line in mailMsg:
if line.find('Subject:')
and then ???
Nov 15 '06 #1
Share this Question
Share on Google+
9 Replies


P: 38
Expand|Select|Wrap|Line Numbers
  1. mailMsg = ['Return-Path: <Louisette.Snoekx@123.com>', 'Delivered-To: 3-marc.jonkers@456.com', 'From: "louisette snoekx" <Louisette.Snoekx@123.com>', 'To: <marc.jonkers@456.com>', 'References: <20060919215507.cvi40klhf1q8884k@webmail.456.com>', 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100']
  2. subjectStr = 'Subject:'
  3. subject = []
  4. for item in mailMsg:
  5.     if item.startswith(subjectStr):
  6.         subject.append(item[len(subjectStr):].strip())
  7. print subject
This will put the subject in a list assuming that mailMsg is a list of strings.

BR
/Fredrik
Nov 15 '06 #2

P: 4
Thanks a lot !
This is aprox what I was looiking for!

The result of the print is for example [' something']
Is it possible to not showing the [' and the '] when printing ?

Second question:
if I do the same for the fromStr (mail comming from ...)
the print result is in this example :
"Louisette ,Snoekx "<Louisette.Snoekx@123.com>
Here I just want to show :Louisette.Snoekx@123.com when printing
so I only want what's between the < and >

Once again thanks a lot for your help !
Nov 15 '06 #3

kudos
Expert 100+
P: 126
Maybe something like this (if I understood the question correctly)

Expand|Select|Wrap|Line Numbers
  1. s = "\"Louisette ,Snoekx \"<Louisette.Snoekx@123.com>"
  2. s =  ((((s.split("\""))[2]).split("<"))[1])
  3. print s[:len(s)-1]
  4.  
-kudos

Thanks a lot !
This is aprox what I was looiking for!

The result of the print is for example [' something']
Is it possible to not showing the [' and the '] when printing ?

Second question:
if I do the same for the fromStr (mail comming from ...)
the print result is in this example :
"Louisette ,Snoekx "<Louisette.Snoekx@123.com>
Here I just want to show :Louisette.Snoekx@123.com when printing
so I only want what's between the < and >

Once again thanks a lot for your help !
Nov 15 '06 #4

P: 4
Maybe something like this (if I understood the question correctly)

Expand|Select|Wrap|Line Numbers
  1. s = "\"Louisette ,Snoekx \"<Louisette.Snoekx@123.com>"
  2. s =  ((((s.split("\""))[2]).split("<"))[1])
  3. print s[:len(s)-1]
  4.  
-kudos
Kudos , thank you , but it doesn't seem to work.
Is there another way to strip everything in front of and including < ?
And is there another way to strip the last > ?
Nov 15 '06 #5

bartonc
Expert 5K+
P: 6,596
Kudos , thank you , but it doesn't seem to work.
Is there another way to strip everything in front of and including < ?
And is there another way to strip the last > ?
There IS another way (there is almost always another way in Python).
Regular Expressions are what you need. Here is a link to a tutorial for using the re module:
http://www.amk.ca/python/howto/regex/.
Basically, (once you understand the syntax) you can tell a re.search() that you want everything that comes after "whatever" and is between "<" and ">" . Have fun and keep posting! Be sure to read the posting guidelines so that you know how to use code tags around the code in your posts. Thanks,
Barton
Nov 15 '06 #6

bartonc
Expert 5K+
P: 6,596
Thanks a lot !
This is aprox what I was looiking for!

The result of the print is for example [' something']
Is it possible to not showing the [' and the '] when printing ?

Second question:
if I do the same for the fromStr (mail comming from ...)
the print result is in this example :
"Louisette ,Snoekx "<Louisette.Snoekx@123.com>
Here I just want to show :Louisette.Snoekx@123.com when printing
so I only want what's between the < and >

Once again thanks a lot for your help !
1)
Expand|Select|Wrap|Line Numbers
  1.  
  2. mailMsg = ['Return-Path: <Louisette.Snoekx@123.com>', 'Delivered-To: 3-marc.jonkers@456.com', 'From: "louisette snoekx" <Louisette.Snoekx@123.com>', 'To: <marc.jonkers@456.com>', 'References: <20060919215507.cvi40klhf1q8884k@webmail.456.com>', 'Subject: Gelezen: workshop 15/10 essen', 'Date: Sun, 5 Nov 2006 22:05:53 +0100']
  3. subjectStr = 'Subject:'
  4. subject = []
  5. for item in mailMsg:
  6.     if item.startswith(subjectStr):
  7.         subject.append(item[len(subjectStr):].strip())
  8. for item in subject:
  9.     print item
  10.  
2)
Expand|Select|Wrap|Line Numbers
  1.  
  2. fromaddress = ""
  3. for item in mailMsg: 
  4.     if item.startswith("From:"):
  5.         i = item.find("<") + 1
  6.         j = item.find(">", i)
  7.         fromaddress = item[i:j]
  8. print fromaddress
  9.  
As I said, there's (almost) always another way in python.
Nov 15 '06 #7

bartonc
Expert 5K+
P: 6,596
here's how to get help at the python prompt:

Expand|Select|Wrap|Line Numbers
  1.  
  2. >>> help(str.find)
  3. Help on method_descriptor: 
  4.  
  5. find(...)
  6.     S.find(sub [,start [,end]]) -> int
  7.  
  8.     Return the lowest index in S where substring sub is found,
  9.     such that sub is contained within s[start,end]. Optional
  10.     arguments start and end are interpreted as in slice notation.
  11.  
  12.     Return -1 on failure.
  13.  
  14.  
Nov 15 '06 #8

P: 4
Thanks a lot bartonc !
Now it works !!
Nov 17 '06 #9

bartonc
Expert 5K+
P: 6,596
Thanks a lot bartonc !
Now it works !!
You are very welcome. Keep posting here on the python forum,
Barton
Nov 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.