def tobytes(thing, encoding='utf-8', errors='strict'): '''Convert string `thing` to byte string `encoding`.''' if isbytes(thing): return thing elif isunicode(thing): return norm(thing).encode(encoding, errors) return utfme(thing).encode(encoding, errors)
def tobytes(thing, encoding="utf-8", errors="strict"): """Convert string `thing` to byte string `encoding`.""" if isbytes(thing): return thing elif isunicode(thing): return norm(thing).encode(encoding, errors) return utfme(thing).encode(encoding, errors)
def tounicode(thing, encoding='utf-8', errors='strict'): '''Convert string `thing` to unicode string with `encoding`.''' if isbytes(thing): try: return thing.decode(encoding, errors) except UnicodeDecodeError: return norm(thing).decode(encoding, errors) elif isunicode(thing): return thing.encode(encoding, errors).decode(encoding, errors) return utfme(thing).encode(encoding, errors).decode(encoding, errors)
def tounicode(thing, encoding="utf-8", errors="strict"): """Convert string `thing` to unicode string with `encoding`.""" if isbytes(thing): try: return thing.decode(encoding, errors) except UnicodeDecodeError: return norm(thing).decode(encoding, errors) elif isunicode(thing): return thing.encode(encoding, errors).decode(encoding, errors) return utfme(thing).encode(encoding, errors).decode(encoding, errors)