raw strings

Michele Simionato mis6 at pitt.edu
Thu Oct 10 13:39:51 EDT 2002


bokr at oz.net (Bengt Richter) wrote in message news:<ao08m6$dpv$0 at 216.39.172.122>...
> >
> No guarantees, but does this do what you want?
> 
>  >>> def raw_string(s):
>  ...     return "r'%s'" % ''.join(
>  ...         [(x,`x`[1:-1],'\\')[`x`[1:].startswith('\\')+(x=='\\')] for x in s]
>  ...     )
>  ...
>  >>> s='\*hello\*\n'
>  >>> print raw_string(s)
>  r'\*hello\*\n'
>  >>> r=r'\*hello\*\n'
>  >>> print raw_string(r)
>  r'\*hello\*\n'
> 
> Regards,
> Bengt Richter

Cool attempt, but doesn't work: for instance 'hel\\lo' is sent in
r'hel\lo' and not in r'hel\\lo'. And there are other drawbacks (try
for instance to apply the function to '\1'). The problems seems much more
complicated than I expected.



More information about the Python-list mailing list