simple string search and replace

K

Kun

hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<[email protected]>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <[email protected]>\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 ['(e-mail address removed)>', '(e-mail address removed)>']
 
B

bearophileHUGS

Generally, to remove a substring (like ">") from a string you can use
the replace method (that returns a new string):
'...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
 
S

Steve Holden

Kun said:
hey guys, here's my code,

senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
<[email protected]>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
(FROM)] {37}', 'From: Kun <[email protected]>\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 ['(e-mail address removed)>', '(e-mail address removed)>']

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
 
D

Dennis Lee Bieber

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 ['(e-mail address removed)>', '(e-mail address removed)>']

Might I suggest reading the Python Tutorial? The sections on string
manipulation, perhaps...

You remove the last character the same way you ignored the first...
my not including it in the range of characters to be copied.

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

NOTE: I put the [:-1] after the .strip() on the presumption that you do
have trailing spaces that have to be removed first, and THEN need to
remove the last one.

If there are no trailing whitespace, you don't need the .strip() and
could use:

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

OR, again if there are no trailing whitespace (and I would hope the
email formatting didn't put whitespace inside the <...>)

parsed_senders.append(sender[sender.find('<'):].strip("<>"))

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top