locale.format()和UnicodeDecodeError:'ascii'编解码器无法解码字节

时间:2018-07-17 14:37:19

标签: python encoding localization format pyramid

我找不到使该代码返回正确的unicode字符串的方法。。。。。。

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

代码在这里:

def format_amount(amount, trim=True, grouping=True, precision=2, dividor=None):
    """
        return a pretty printable amount
    """
    resp = u""
    if amount is not None:
        if dividor is None:
            dividor = 10.0 ** precision

        # Limit to 2 trailing zeros
        if isinstance(amount, float) and precision <= 2:
            if amount == int(amount):
                trim = True
        elif precision > 2:
            if math_utils.floor_to_precision(
                amount,
                precision=2,
                dialect_precision=precision
            ) == amount:
                trim = True

        if trim:
            formatter = "%.2f"
            amount = int(amount) / dividor
            resp = locale.format(formatter, amount, grouping=grouping)
        else:
            formatter = "%.{0}f".format(precision)
            amount = amount / dividor
            resp = locale.format(formatter, amount, grouping=grouping)
            resp = resp.rstrip('0')
            resp = add_trailing_zeros(resp)

    logger.debug('{}'.format(locale.getlocale()))
    logger.debug('amount initial:{} - reformaté:{} [{}]'.format(amount, resp, type(resp)))
    if grouping:
        resp = resp.replace(' ', '&nbsp;')
    return resp

记录器返回:

127.0.0.1 - 2018-07-17 16:35:13,601 DEBUG ('fr_FR', 'UTF-8') 
127.0.0.1 - 2018-07-17 16:35:13,602 DEBUG amount initial:9000.0 - reformaté:9 000,00 [<type 'str'>]

我不知道如何使locale.format发回一个很好的unicode字符串...

0 个答案:

没有答案