2009/5/19 spir <denis.s...@free.fr>: > def _cleanRepr(text): > ''' text with control chars replaced by repr() equivalent ''' > result = "" > for char in text: > n = ord(char) > if (n < 32) or (n > 126 and n < 160): > char = repr(char)[1:-1] > result += char > return result
Is this faster? It replaces all occurrences of each control character in ctrlcmap for the whole string you pass to it. ctrlcmap = dict((chr(x), repr(chr(x))[1:-1]) for x in range(160) if x < 32 or x > 126 and x < 160) teststring = chr(127) + 'mytestring' def _cleanRepr(text, ctrlcmap): for ctrlc in ctrlcmap.keys(): text = text.replace(ctrlc, ctrlcmap[ctrlc]) return text >>> teststring '\x7fmytestring' >>> _cleanRepr(teststring, ctrlcmap) '\\x7fmytestring' Greets Sander _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor