Redefining "\n"

Jason Orendorff jason at jorendorff.com
Sun Feb 17 14:51:50 EST 2002


Dale Strickland-Clark wrote:
> Is there some way to redefine the "\n" escape sequence?
> 
> When preparing strings for the Windows clipboard, I need \x0d\x0a for
> a newline which is a bit cumbersome to insert all over the place.
> 
> It would be neat if I could make "\n" insert some other sequence.
> 
> I know I can do a translation before sending the string to the
> clipboard but this would be nicer.

I know what you're asking... but I can't help thinking that maybe
what you really want is:

def clip(s):
    s = s.replace('\n', '\r\n')
    import win32clipboard as cb
    cb.OpenClipboard()
    cb.EmptyClipboard()
    cb.SetClipboardData(cb.CF_TEXT, s)
    cb.CloseClipboard()

Calling into Win32 is the cumbersomeness-determining step.

## Jason Orendorff    http://www.jorendorff.com/




More information about the Python-list mailing list