simple string backspace question

Diez B. Roggisch deets at nospam.web.de
Tue Jul 31 06:37:07 EDT 2007


 vedrandekovic at v-programs.com wrote:

> On 31 srp, 12:03, ra... at dot.com (Lawrence Oluyede) wrote:
>> <vedrandeko... at v-programs.com> wrote:
>> > If you mean on operating system then unfortunately Windows XP.
>>
>> I don't know for sure but maybe it doesn't support all ASCII escapes
>> codes.
>>
>> Why do you care about \b anyway :-) ?
>>
>> --
>> Lawrence, oluyede.org - neropercaso.it
>> "It is difficult to get a man to understand
>> something when his salary depends on not
>> understanding it" - Upton Sinclair
> 
> Hi,
> 
> I need this inevitable for my "programming language", for code
> indentation. I don't know how to  write script with module tokenize
> for code indentation.

Still not giving up reinventing the wheel? You should take some lessons on
syntax analysis before attempting this. But I know this words won't be
heard...

So, to your actual problem: that backspace is removing a character is
something an editor or a terminal do, because they interpret the backspace.
You wouldn't expect the string "<font color="blue">foo</font>" to be
rendered blue by magic as well, wouldn't you?

So what you need to do is: search the string for backspaces, and remove the
BS as well as the character before. Something along these lines (untested):

teststring = "abc\bcde\b"

while teststring.find("\b") > -1:
      pos = teststring.find("\b")
      teststring = teststring[:pos-1] + teststring[pos+1:]


Diez



More information about the Python-list mailing list