simple string search and replace

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kun

    #1

    simple string search and replace

    hey guys, here's my code,

    senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
    <anon@anon.whar ton.com>\r\n\r\ n'), ')', ('462 (BODY[HEADER.FIELDS
    (FROM)] {37}', 'From: Kun <neurogasm@gmai l.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 ['anon@anon.whar ton.com>', 'neurogasm@gmai l.com>']
  • bearophileHUGS@lycos.com

    #2
    Re: simple string search and replace

    Generally, to remove a substring (like ">") from a string you can use
    the replace method (that returns a new string):
    [color=blue][color=green][color=darkred]
    >>> s = "...anon.wharto n.com>..."
    >>> s.replace(">", "")[/color][/color][/color]
    '...anon.wharto n.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

    Comment

    • Steve Holden

      #3
      Re: simple string search and replace

      Kun wrote:[color=blue]
      > hey guys, here's my code,
      >
      > senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
      > <anon@anon.whar ton.com>\r\n\r\ n'), ')', ('462 (BODY[HEADER.FIELDS
      > (FROM)] {37}', 'From: Kun <neurogasm@gmai l.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 ['anon@anon.whar ton.com>', 'neurogasm@gmai l.com>'][/color]

      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.blogs pot.com

      Comment

      Working...