Converstion

John Machin sjmachin at lexicon.net
Thu Apr 27 22:20:19 EDT 2006


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).



More information about the Python-list mailing list