Converstion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    #1

    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.

  • placid

    #2
    Re: Converstion


    Chris wrote:[color=blue]
    > 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.[/color]

    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

    Comment

    • John Machin

      #3
      Re: Converstion

      On 28/04/2006 9:50 AM, Chris wrote:[color=blue]
      > 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.[/color]

      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\x08ll o"??

      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?
      [color=blue]
      > 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.
      >[/color]

      If, as you say, the bad character is omitted, what do you think is wrong
      with input_string.re place("\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_lis t).

      Comment

      • Paddy

        #4
        Re: Converstion

        Something like (untested):

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

        - Pad.

        Comment

        • John Machin

          #5
          Re: Converstion

          On 28/04/2006 4:46 PM, Paddy wrote:[color=blue]
          > Something like (untested):
          >
          > out = []
          > for ch in instring:
          > if ch==backspace:
          > if out:
          > out = out[:-1]
          > else:
          > out.append(ch)
          > outstring = ''.join(out)[/color]

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

          Comment

          • Paddy

            #6
            Re: Converstion

            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.

            Comment

            • Peter Otten

              #7
              Re: Converstion

              Paddy wrote:
              [color=blue]
              > the del version - is that an optimisation?
              > Is it actually faster?[/color]

              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

              Comment

              • Edward Elliott

                #8
                Re: Converstion

                Peter Otten wrote:[color=blue]
                > 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:[/color]

                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"*10 00' '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

                Comment

                • John Machin

                  #9
                  Re: Converstion

                  On 29/04/2006 2:22 AM, Edward Elliott wrote:[color=blue]
                  > Peter Otten wrote:[color=green]
                  >> 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:[/color]
                  >
                  > But his data is a string, which is immutable but heavily optimized:[/color]

                  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.





                  Comment

                  Working...