simple string search and replace

Discussion in 'Python' started by Kun, Mar 25, 2006.

  1. Kun

    Kun Guest

    hey guys, here's my code,

    senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
    <>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
    (FROM)] {37}', 'From: Kun <>\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 ['>', '>']
    Kun, Mar 25, 2006
    #1
    1. Advertising

  2. Kun

    Guest

    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, 2006
    #2
    1. Advertising

  3. Kun

    Steve Holden Guest

    Kun wrote:
    > hey guys, here's my code,
    >
    > senders = [('460 (BODY[HEADER.FIELDS (FROM)] {46}', 'From: Friend
    > <>\r\n\r\n'), ')', ('462 (BODY[HEADER.FIELDS
    > (FROM)] {37}', 'From: Kun <>\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 ['>', '>']


    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
    Steve Holden, Mar 25, 2006
    #3
  4. On Sat, 25 Mar 2006 15:53:47 -0500, Kun <> declaimed
    the following in comp.lang.python:


    > 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 ['>', '>']


    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("<>"))

    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Mar 25, 2006
    #4
  5. On Sat, 25 Mar 2006 23:54:52 GMT, Dennis Lee Bieber
    <> declaimed the following in comp.lang.python:

    Pardon...

    > my not including it in the range of characters to be copied.
    >

    by not...
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
    Dennis Lee Bieber, Mar 26, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alun
    Replies:
    3
    Views:
    4,496
    Masudur
    Feb 18, 2008
  2. Gontrand Trudau
    Replies:
    0
    Views:
    281
    Gontrand Trudau
    Jan 13, 2010
  3. Prasad S
    Replies:
    2
    Views:
    226
    Dr John Stockton
    Aug 27, 2004
  4. sivga
    Replies:
    2
    Views:
    104
    John W. Krahn
    Nov 7, 2007
  5. SM
    Replies:
    4
    Views:
    208
Loading...

Share This Page