I have a string
s = '--two \x08--three'
When I print, I get
--two--three
but can I do something to have
s='--two--three'
with out declaring it explicitly.
I do not need to store or even know about the back spaces. I just want to manipulate the text without the backspace characters there. How can I achieve this?
Edit: Hopefully I can clarify a little. Say I have two strings
test1 = 'a\bb' #b
test2 = 'b' #b
When they are printed, they are equivalent to the user, but test1!=test2
. What I am doing is pulling some output from a terminal. This output has backspaces throughout. I want to be able to manipulate the end result, search for words, edit the string without worrying about the backspaces.
Edit 2: I guess what I really want is to set a variable as the result of a print statement
a = print("5\bA") #a='A' #does not work like this
You can apply backspaces to a string using regular expressions:
import re
def apply_backspace(s):
while True:
# if you find a character followed by a backspace, remove both
t = re.sub('.\b', '', s, count=1)
if len(s) == len(t):
# now remove any backspaces from beginning of string
return re.sub('\b+', '', t)
s = t
Now:
>>> apply_backspace('abc\b\b\b123')
'123'