Escapeism
Frederic Rentsch
anthra.norell at vtxmail.ch
Sun Oct 1 03:58:49 EDT 2006
Kay Schluehr wrote:
> Sybren Stuvel wrote:
>
>> Kay Schluehr enlightened us with:
>>
>>> Usually I struggle a short while with \ and either succeed or give up.
>>> Today I'm in a different mood and don't give up. So here is my
>>> question:
>>>
>>> You have an unknown character string c such as '\n' , '\a' , '\7' etc.
>>>
>>> How do you echo them using print?
>>>
>>> print_str( c ) prints representation '\a' to stdout for c = '\a'
>>> print_str( c ) prints representation '\n' for c = '\n'
>>> ...
>>>
>>> It is required that not a beep or a linebreak shall be printed.
>>>
>> try "print repr(c)".
>>
>
> This yields the hexadecimal representation of the ASCII character and
> does not simply echo the keystrokes '\' and 'a' for '\a' ignoring the
> escape semantics. One way to achieve this naturally is by prefixing
> '\a' with r where r'\a' indicates a "raw" string. But unfortunately
> "rawrification" applies only to string literals and not to string
> objects ( such as c ). I consider creating a table consisting of pairs
> {'\0': r'\0','\1': r'\1',...} i.e. a handcrafted mapping but maybe
> I've overlooked some simple function or trick that does the same for
> me.
>
> Kay
>
>
Kay,
This is perhaps yet another case for SE? I don't really know, because I
don't quite get what you're after. See for yourself:
>>> import SE
>>> Printabilizer = SE.SE ( '''
(1)=\\1 # All 256 octets can be written as parenthesized ascii
(2)=\\2
"\a=\\a" # (7)=\\a"
"\n=\\n" # or (10)=\\n or (10)=LF or whatever
"\r=\\r" # (13)=CR
"\f=\\f"
"\v=\\v"
# Add whatever other ones you like
# and translate them to anything you like.
''')
>>> print Printabilizer ('abd\aefg\r\nhijk\vlmnop\1\2.')
abd\aefg\r\nhijk\vlmno\1\2.
If you think this may help, you'll find SE here:
http://cheeseshop.python.org/pypi/SE/2.2%20beta
Regards
Frederic
More information about the Python-list
mailing list