470,849 Members | 697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,849 developers. It's quick & easy.

Converstion

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.

Apr 28 '06 #1
8 2523

Chris wrote:
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

Apr 28 '06 #2
On 28/04/2006 9:50 AM, Chris wrote:
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).
Apr 28 '06 #3
Something like (untested):

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

- Pad.

Apr 28 '06 #4
On 28/04/2006 4:46 PM, Paddy wrote:
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:]
Apr 28 '06 #5
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.

Apr 28 '06 #6
Paddy wrote:
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
Apr 28 '06 #7
Peter Otten wrote:
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

Apr 28 '06 #8
On 29/04/2006 2:22 AM, Edward Elliott wrote:
Peter Otten wrote:
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.

Apr 28 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Cheryl | last post: by
2 posts views Thread by Christopher Weaver | last post: by
3 posts views Thread by Andre Ranieri | last post: by
reply views Thread by Sehboo | last post: by
1 post views Thread by Sehboo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.