Converstion

C

Chris

In a program I'm writing I have a problem where a bit of text sent over
a network arrives at my server. If the person who sent the text made a
mistake typing the word and pressed backspace the backspace code is
included in the word for example hello is hel\x08lo. The \x08 is the
backspace key. How do I convert this string to a normal string (without
the \x08). If I print it to screen it appears normal, "hello" but if I
store it in a list it appears as hel\x08lo.
 
P

placid

Chris said:
In a program I'm writing I have a problem where a bit of text sent over
a network arrives at my server. If the person who sent the text made a
mistake typing the word and pressed backspace the backspace code is
included in the word for example hello is hel\x08lo. The \x08 is the
backspace key. How do I convert this string to a normal string (without
the \x08). If I print it to screen it appears normal, "hello" but if I
store it in a list it appears as hel\x08lo.

inefficient way, use split ("\x08") then recombine the splitted
elements, or iterate thorough the list and create a new string, but
dont add the backspace character
 
J

John Machin

In a program I'm writing I have a problem where a bit of text sent over
a network arrives at my server. If the person who sent the text made a
mistake typing the word and pressed backspace the backspace code is
included in the word for example hello is hel\x08lo.

Interesting. If the sender typed say ";" instead of the second "l", then
corrected it, one would expect either the raw (in Unix terminology)
string "hel;\x08lo" or the cooked string "hello". What network is that?
What protocol is being used for sending user input?

What happens if the user backspaces TWICE e.g. raw input at keyboard is
"he;;\x08\x08llo"??

What other funny business could be going on that you haven't stumbled on
yet? Can the user cancel a whole line by keying say Ctrl-X? If so, what
happens?
The \x08 is the
backspace key. How do I convert this string to a normal string (without
the \x08). If I print it to screen it appears normal, "hello" but if I
store it in a list it appears as hel\x08lo.

If, as you say, the bad character is omitted, what do you think is wrong
with input_string.replace("\x08", "") ?

If the bad characters are not omitted, you would have to work a bit
harder: step through the characters, appending them to a list. When you
hit a backspace, delete the last character in the list (if any). At the
end, do "".join(the_list).
 
P

Paddy

Something like (untested):

out = []
for ch in instring:
if ch==backspace:
if out:
out = out[:-1]
else:
out.append(ch)
outstring = ''.join(out)

- Pad.
 
J

John Machin

Something like (untested):

out = []
for ch in instring:
if ch==backspace:
if out:
out = out[:-1]
else:
out.append(ch)
outstring = ''.join(out)

Instead of:
if out:
out = out[:-1]
consider:
del out[-1:]
 
P

Paddy

the del version - is that an optimisation?
Is it actually faster?
- I did not have enough info. to check so just did what came naturally
to me :)

- Pad.
 
P

Peter Otten

Paddy said:
the del version - is that an optimisation?
Is it actually faster?

del x[-1:] # or del x[-1] if you are sure that len(x) > 0

just deletes the last item (if any) from x whereas

x = x[:-1]

copies all but the last item of the original list into a new one. This can
take much longer:

[copy 10000 lists with 5000 items on average]
$ python -m timeit -n10000 -s'data = range(10000)' 'data = data[:-1]'
10000 loops, best of 3: 38.9 usec per loop

[remove the last item from a list 10000 times]
$ python -m timeit -n10000 -s'data = range(10000)' 'del data[-1:]'
10000 loops, best of 3: 0.272 usec per loop
$ python -m timeit -n10000 -s'data = range(10000)' 'del data[-1]'
10000 loops, best of 3: 0.246 usec per loop

Peter
 
E

Edward Elliott

Peter said:
del x[-1:] # or del x[-1] if you are sure that len(x) > 0
just deletes the last item (if any) from x whereas
x = x[:-1]
copies all but the last item of the original list into a new one. This can
take much longer:

But his data is a string, which is immutable but heavily optimized:

$ python -m timeit -n10000 -s'data = range(10000)' 'data = data[:-1]'
10000 loops, best of 3: 41.9 usec per loop

$python -m timeit -n10000 -s'data = range(10000)' 'del data[-1:]'
10000 loops, best of 3: 0.244 usec per loop

$ python -m timeit -n10000 -s'data = "abcdefghij"*1000' 'data = data[:-1]'
10000 loops, best of 3: 1.7 usec per loop

$ python -m timeit -n10000 -s'data = "abcdefghijklm"*1000' 'del data[-1:]'
[traceback omitted]
TypeError: object doesn't support slice deletion
 
J

John Machin

Peter said:
del x[-1:] # or del x[-1] if you are sure that len(x) > 0
just deletes the last item (if any) from x whereas
x = x[:-1]
copies all but the last item of the original list into a new one. This can
take much longer:

But his data is a string, which is immutable but heavily optimized:

Sorry, my mistake, I could have sworn it was a list:
"""
out = []
for ch in instring:
if ch==backspace:
if out:
out = out[:-1]
else:
out.append(ch)
outstring = ''.join(out)
"""

See that [] in the first line? That's what's confusing me. The
out.append in the 2nd last line adds to the bogglement.
 

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
474,294
Messages
2,571,511
Members
48,200
Latest member
SCPKatheri

Latest Threads

Top