Beispiel #1
0
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)
Beispiel #2
0
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)
Beispiel #3
0
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)
Beispiel #4
0
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)