Removing a substring from a string

Discussion in 'Python' started by ankit, Jan 16, 2006.

  1. ankit

    ankit Guest

    Hi All,
    I want to remove a substring from a string without any additional
    tabs/returns in the output string. Is there any method availaible or
    how can I do it. For the ease, I am giving an example:

    Code:
    mainstr ="""
    ${if:isLeaf}
      Dont include this isLeaf=True
    ${/if:isLeaf}
    
    ${if:isStatic}
      include this isStatic=True
    ${/if:isStatic}
    
    ${if:!isLeaf}
       include this isLeaf=False
    ${/if:!isLeaf}
    """
    
    Now if I want to remove :
    Code:
    substr = ""
    ${if:isLeaf}
      Dont include this isLeaf=True
    ${/if:isLeaf}
    """
    
    Expected output is :
    Code:
    ${if:isStatic}
      include this isStatic=True
    ${/if:isStatic}
    
    ${if:!isLeaf}
       include this isLeaf=False
    ${/if:!isLeaf}
    
    I am using mainstr.replace(substr, "") but it gives me additional
    carriage returns which leads to empty spaces as follows:

    Actual Output:
    Code:
    
    
    
    
    
    ${if:isStatic}
      include this isStatic=True
    ${/if:isStatic}
    
    ${if:!isLeaf}
       include this isLeaf=False
    ${/if:!isLeaf}
    
    Note: See additional newlines before isStatic tag


    Any Suggestions ?
    ankit, Jan 16, 2006
    #1
    1. Advertising

  2. ankit

    Peter Hansen Guest

    ankit wrote:
    > I am using mainstr.replace(substr, "") but it gives me additional
    > carriage returns which leads to empty spaces as follows:


    The .replace() method does *not* introduce additional carriage returns
    (nor newlines/linefeeds, which is probably what you meant). If you
    think it does, your tests are flawed or you are misinterpreting what is
    happening.

    Maybe start with a simpler test case, and make use of repr() on your
    output to let you compare the input and output more accurately.

    If you still think newlines are being introduced, please post the
    *actual code* you are using. Do not just retype it: cut and paste it so
    we know it's reliable.

    (I tried with what you posted, and after correcting for the missing
    quotation mark when you defined substr, I get the results you wanted,
    not what you showed.)

    -Peter
    Peter Hansen, Jan 16, 2006
    #2
    1. Advertising

  3. ankit

    Paul Watson Guest

    ankit wrote:
    > Hi All,
    > I want to remove a substring from a string without any additional
    > tabs/returns in the output string. Is there any method availaible or
    > how can I do it. For the ease, I am giving an example:
    >
    >
    Code:
    > mainstr ="""
    > ${if:isLeaf}
    >   Dont include this isLeaf=True
    > ${/if:isLeaf}
    > 
    > ${if:isStatic}
    >   include this isStatic=True
    > ${/if:isStatic}
    > 
    > ${if:!isLeaf}
    >    include this isLeaf=False
    > ${/if:!isLeaf}
    > """
    > 
    >
    > Now if I want to remove :
    >
    Code:
    > substr = ""
    > ${if:isLeaf}
    >   Dont include this isLeaf=True
    > ${/if:isLeaf}
    > """
    > 
    >
    > Expected output is :
    >
    Code:
    > ${if:isStatic}
    >   include this isStatic=True
    > ${/if:isStatic}
    > 
    > ${if:!isLeaf}
    >    include this isLeaf=False
    > ${/if:!isLeaf}
    > 
    >
    > I am using mainstr.replace(substr, "") but it gives me additional
    > carriage returns which leads to empty spaces as follows:
    >
    > Actual Output:
    >
    Code:
    > 
    > 
    > 
    > 
    > 
    > ${if:isStatic}
    >   include this isStatic=True
    > ${/if:isStatic}
    > 
    > ${if:!isLeaf}
    >    include this isLeaf=False
    > ${/if:!isLeaf}
    > 
    >
    > Note: See additional newlines before isStatic tag
    >
    >
    > Any Suggestions ?


    Are you using 'print' to get the output? Please see the following code
    and note what the 'print' lines which have a comma at the end produce.
    The 'print' statement is not always suitable for producing closely
    controlled output. Consider constructing the exact string you want and
    using a 'write' method.

    $ cat ./jjj.py
    #!/usr/bin/python

    s = 'now'
    print s
    print s,
    print s

    x = s.replace('now', '')
    print '%s' % x,
    print 'then'
    09:34 pwatson [ ruth:/home/pwatson ] 431
    $ ./jjj.py
    now
    now now
    then
    Paul Watson, Jan 16, 2006
    #3
    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. Chris  Chiasson
    Replies:
    6
    Views:
    602
    Richard Tobin
    Nov 14, 2006
  2. becte

    removing substring from string

    becte, Feb 27, 2005, in forum: C Programming
    Replies:
    7
    Views:
    368
    Lawrence Kirby
    Feb 28, 2005
  3. Jack

    Removing substring

    Jack, Jan 26, 2008, in forum: C++
    Replies:
    4
    Views:
    330
    Erik Wikström
    Jan 27, 2008
  4. Replies:
    3
    Views:
    188
    Sherm Pendley
    Aug 3, 2005
  5. SM
    Replies:
    4
    Views:
    201
Loading...

Share This Page