Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 38 lines (33 sloc) 1.12 KB
#Copyright (c) 2011 Yahoo! Inc. All rights reserved. Licensed under the BSD License.
# See accompanying LICENSE file or for the specific language governing permissions and limitations under the License.
__author__ = "BOSS Team"
def strfix(msg):
Copies this recipe:
Constants out any characters that spew encoding errors
goodchars = {}
return str(msg)
except UnicodeEncodeError:
for i in list(msg):
if i not in goodchars:
goodchars[i] = i
except UnicodeEncodeError:
# format character as python string constant
code = ord(i)
t = None
if code < 256:
t ='\\x%02x' % code # 8-bit value
elif code < 65536:
t ='\\u%04x' % code # 16-bit value unicode
t = '\\U%08x' % code # other values as 32-bit unicode
goodchars[i] = t # or '.' for readability ;-)
res += goodchars[i]
return res
def write(msg):
print strfix(msg)