def safe_str(obj):
    """ return the byte string representation of obj """
    try:
        return str(obj)
    except UnicodeEncodeError:
        # obj is unicode
        return unicode(obj).encode('unicode_escape')
 return the byte string representation of obj 
recipes/Python/466341_Guaranteed_conversiunicode_or_byte/recipe-466341.py