Removing a substring from a string

A

ankit

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 ?
 
P

Peter Hansen

ankit said:
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
 
P

Paul Watson

ankit said:
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
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top