BlaY queremos representarlo todo en una línea sin perder información, como hacerlo?
blablabla\ /
blabla
def miniscape(s):
return str([s])[2:-2]
Resultado:
\nBla\nblablabla\\ /\nblabla\n
Y para darle la vuelta?
def miniunscape(s):
i = 0
while True:
try:
x = s.index("\\x", i)
if not((x > 0) and (s[x-1] == "\\")):
s = s[:x]+chr(int(s[x+2:x+4],16))+s[x+4:]
i = x + 1
except:
break
reptable = [("\\\\","\\"),("\\n","\n"),("\\'","'"),("\\a","\a"),
("\\b","\b"),("\\f","\f"),("\\r","\r"),("\\t","\t"),
("\\v","\v")]
for r in reptable:
i = 0
while True:
try:
x = s.index(r[0], i)
if not((x > 0) and (s[x-1] == "\\")):
s = s[:x]+r[1]+s[x+2:]
i = x + 1
except:
break
return s
En pastebin [ http://pastebin.com/5uvf3tBK ]
Y ya está, nos vemos
No hay comentarios:
Publicar un comentario