Message115915
How about the following solution:
>>> def a(s):
... s = s.encode('unicode-escape').decode('ascii')
... s = s.replace("'", r"\'")
... return "'" + s + "'"
...
>>> s = "'\0\"\n\r\t abcd\x85é\U00012fff\U0001D121xxx\uD800."
>>> print(ascii(s)); print(a(s)); print(repr(s))
'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\ud834\udd21xxx\ud800.'
'\'\x00"\n\r\t abcd\x85\xe9\U00012fff\U0001d121xxx\ud800.'
'\'\x00"\n\r\t abcd\x85é\U00012fff𝄡xxx\ud800.'
(I think I've included everything:
- normal chars
- control chars
- one-byte non-ASCII
- two-byte non-ASCII (and lone surrogate)
- printable and non-printable surrogate pairs)
- single and double quotes) |
|
Date |
User |
Action |
Args |
2010-09-08 23:04:56 | pitrou | set | recipients:
+ pitrou, vstinner |
2010-09-08 23:04:53 | pitrou | set | messageid: <1283987093.93.0.563531611442.issue9804@psf.upfronthosting.co.za> |
2010-09-08 23:04:52 | pitrou | link | issue9804 messages |
2010-09-08 23:04:51 | pitrou | create | |
|