470,827 Members | 1,724 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,827 developers. It's quick & easy.

simple string search and replace

Kun
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<an**@anon.wharton.com>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmail.com>\r\n\r\n'), ')']
print senders
parsed_senders = []
sender = ""
for item in senders:
if isinstance(item,tuple):
item= ''.join(item)
if item==')':
parsed_senders.append(sender[sender.find('<')+1:].strip())
sender = ""
else:
sender+=item
print parsed_senders


wondering if anyone knows how i can remove the '>'s from the list, which
outputs to something like ['an**@anon.wharton.com>', 'ne*******@gmail.com>']
Mar 25 '06 #1
2 1051
Generally, to remove a substring (like ">") from a string you can use
the replace method (that returns a new string):
s = "...anon.wharton.com>..."
s.replace(">", "")

'...anon.wharton.com...'

You can use it with something like:
print [s.replace(">", "") for s in parsed_senders]

or you can put the replace() somewhere in the main loop.

Probably to solve your problem there are other solutions, like using a
RE to find email addresses inside the string...

Bye,
bearophile

Mar 25 '06 #2
Kun wrote:
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<an**@anon.wharton.com>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <ne*******@gmail.com>\r\n\r\n'), ')']
print senders
parsed_senders = []
sender = ""
for item in senders:
if isinstance(item,tuple):
item= ''.join(item)
if item==')':
parsed_senders.append(sender[sender.find('<')+1:].strip())
sender = ""
else:
sender+=item
print parsed_senders


wondering if anyone knows how i can remove the '>'s from the list, which
outputs to something like ['an**@anon.wharton.com>', 'ne*******@gmail.com>']


Where you append to parsed_senders, replace

sender[sender.find('<')+1:]

with

sender[sender.find('<')+1:-1]

and that will use a string one shorter, omitting the ">" character.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd www.holdenweb.com
Love me, love my blog holdenweb.blogspot.com

Mar 25 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by C L Humphreys | last post: by
2 posts views Thread by gyromagnetic | last post: by
4 posts views Thread by beliavsky | last post: by
1 post views Thread by timothy ma and constance lee | last post: by
4 posts views Thread by suzy | last post: by
21 posts views Thread by gary | last post: by
1 post views Thread by DougJrs | last post: by
3 posts views Thread by Hvid Hat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.