Help me on Backspace please

C

cakomo

Hi
I am a beginner on Python and have a problem..

I have text file and reading it line by line and there are backspace
characters in it like '\b' or anything you want like "#". I want to
replace these chars. with Backspace action. I mean deleting the
previous char. and the \b char also. and writing all cleaned text to a
file again.

How can I do that.

Thanks..
 
J

John Machin

Hi
I am a beginner on Python and have a problem..

I have text file and reading it line by line and there are backspace
characters in it like '\b' or anything you want like "#". I want to
replace these chars. with Backspace action. I mean deleting the
previous char. and the \b char also. and writing all cleaned text to a
file again.

How can I do that.

I haven't seen anything like that for ... ummm, a very long time. Used
for bolding and making up your own characters on a daisy-wheel
printer. Where did you get the file from?

If there are no cases of multiple adjacent backspaces (e.g. "blahfoo\b
\b\bblah") you can do:
new_line = re.sub(r'.\x08', old_line, '')

Note: using \x08 for backspace instead of \b to avoid having to worry
about how many \ to use in the regex :)

Otherwise you would need to do something like
while True:
new_line = re.sub(r'[^\x08]\x08', '', old_line)
if new_line == old_line: break
old_line = new_line

And if you were paranoid, you might test for any remaining stray
backspaces, just in case the line contains "illegal" things like
"\bfoo" or "foo\b\b\b\b" etc.

Cheers,
John
 
C

cakomo

Hi
I am a beginner on Python and have a problem..
I have text file and reading it line by line and there are backspace
characters in it like '\b' or anything you want like "#".  I want to
replace these chars. with Backspace action. I mean deleting the
previous char. and the \b char also. and writing all cleaned text to a
file again.
How can I do that.

I haven't seen anything like that for ... ummm, a very long time. Used
for bolding and making up your own characters on a daisy-wheel
printer. Where did you get the file from?

If there are no cases of multiple adjacent backspaces (e.g. "blahfoo\b
\b\bblah") you can do:
   new_line = re.sub(r'.\x08', old_line, '')

Note: using \x08 for backspace instead of \b to avoid having to worry
about how many \ to use in the regex :)

Otherwise you would need to do something like
   while True:
      new_line = re.sub(r'[^\x08]\x08', '', old_line)
      if new_line == old_line: break
      old_line = new_line

And if you were paranoid, you might test for any remaining stray
backspaces, just in case the line contains "illegal" things like
"\bfoo" or "foo\b\b\b\b" etc.

Cheers,
John

Thanks John
I will try it but, do you think regex replacement gonna erase the
prev. char.?

Thanks a lot for your reply.
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top