Escapes???

Robert Kern kern at caltech.edu
Fri Jun 2 16:20:41 EDT 2000


In article <8h8jg3$d66$1 at slb6.atl.mindspring.net>,
	aahz at netcom.com (Aahz Maruch) writes:
> In article <8h87u1$sga$1 at news2atm.raleigh.ibm.com>,
> Arinté <shouldbe at message.com> wrote:
>>
>>I am trying to write this escape seq to a device from python
>>"H\x1b\x23\x12art\x1b\x23\x01\n"
>>The \x1b\x23\x12 is supposed to be a command tell the device to do something
>>special with "art", but python is making it look like this \x1b\x23\x12a
>>"rt".  Am I missing something on how to work with escape sequences?  Is
>>there another way to write hex values in a string.  I rather us \x00 because
>>that is what are testers are familiar with.
> 
> I'd like to see some of the actual code.  Firing up the Python
> interpreter produces correct resulte for me.

It doesn't produce the desired result for me.

Python 1.5.2 (#0, Apr  3 2000, 14:46:48)  [GCC 2.95.2 20000313 (Debian GNU/Linux)] on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> s="H\x1b\x23\x12art\x1b\x23\x01\n"
>>> s
'H\033#*rt\033#\001\012'
>>> print s
Hrt

>>> '\x12'
'\022'
>>> '\x12a'
'*'
>>> chr(0x12a % 256)
'*'

The 'a' disappears into the escape sequence, which is not the behaviour that
was desired.

>From http://www.python.org/doc/current/ref/strings.html :

"""\
In strict compatibility with Standard C, up to three octal digits are 
accepted, but an unlimited number of hex digits is taken to be part of the 
hex escape (and then the lower 8 bits of the resulting hex number are used 
in 8-bit implementations). 
"""

A possible solution is the following

>>> s = "H\x1b\x23\x12%s\x1b\x23\x01\n" % 'art'
>>> s
'H\033#\022art\033#\001\012'

It's kludgy (depending on the use; if 'art' isn't the only thing you can put
in that string, then it's actually more flexible this way), but it should
work.

> --
>                       --- Aahz (Copyright 2000 by aahz at netcom.com)

-- 
Robert Kern
kern at caltech.edu

"In the fields of hell where the grass grows high
 Are the graves of dreams allowed to die."
  -- Richard Harter



More information about the Python-list mailing list