您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在Python中将货币解析为数字

在Python中将货币解析为数字

babel文档指出,数字解析还没有完全实现,但是他们为将货币信息输入库中做了很多工作。您可以使用get_currency_name()get_currency_symbol()获取货币详细信息,还可以使用所有其他get_...功能获取常规数字详细信息(小数点,减号等)。

使用该信息,您可以从货币字符串中排除货币详细信息(名称,符号)和分组(例如,在美国)。然后,将小数位详细信息更改为C语言环境所使用的详细信息(-减号和.小数点)。

这样就产生了这段代码(我添加一个对象来保留一些数据,在后续处理中可能会很方便):

import re, os
from babel import numbers as n
from babel.core import default_locale

class AmountInfo(object):
    def __init__(self, name, symbol, value):
        self.name = name
        self.symbol = symbol
        self.value = value

def parse_currency(value, cur):
    decp = n.get_decimal_symbol()
    plus = n.get_plus_sign_symbol()
    minus = n.get_minus_sign_symbol()
    group = n.get_group_symbol()
    name = n.get_currency_name(cur)
    symbol = n.get_currency_symbol(cur)
    remove = [plus, name, symbol, group]
    for token in remove:
        # remove the pieces of information that shall be obvIoUs
        value = re.sub(re.escape(token), '', value)
    # change the minus sign to a LOCALE=C minus
    value = re.sub(re.escape(minus), '-', value)
    # and change the decimal mark to a LOCALE=C decimal point
    value = re.sub(re.escape(decp), '.', value)
    # just in case remove extraneous spaces
    value = re.sub('\s+', '', value)
    return AmountInfo(name, symbol, value)

#cur_loc = os.environ['LC_ALL']
cur_loc = default_locale()
print('locale:', cur_loc)
test = [ (n.format_currency(123456.789, 'USD', locale=cur_loc), 'USD')
       , (n.format_currency(-123456.78, 'PLN', locale=cur_loc), 'PLN')
       , (n.format_currency(123456.789, 'PLN', locale=cur_loc), 'PLN')
       , (n.format_currency(123456.789, 'IDR', locale=cur_loc), 'IDR')
       , (n.format_currency(123456.789, 'JPY', locale=cur_loc), 'JPY')
       , (n.format_currency(-123456.78, 'JPY', locale=cur_loc), 'JPY')
       , (n.format_currency(123456.789, 'CNY', locale=cur_loc), 'CNY')
       , (n.format_currency(-123456.78, 'CNY', locale=cur_loc), 'CNY')
       ]

for v,c in test:
    print('As currency :', c, ':', v.encode('utf-8'))
    info = parse_currency(v, c)
    print('As value    :', c, ':', info.value)
    print('Extra info  :', info.name.encode('utf-8')
                         , info.symbol.encode('utf-8'))

输出看起来很有希望(在美国语言环境中):

$ export LC_ALL=en_US
$ ./cur.py
locale: en_US
As currency : USD : b'$123,456.79'
As value    : USD : 123456.79
Extra info  : b'US Dollar' b'$'
As currency : PLN : b'-z\xc5\x82123,456.78'
As value    : PLN : -123456.78
Extra info  : b'Polish Zloty' b'z\xc5\x82'
As currency : PLN : b'z\xc5\x82123,456.79'
As value    : PLN : 123456.79
Extra info  : b'Polish Zloty' b'z\xc5\x82'
As currency : IDR : b'Rp123,457'
As value    : IDR : 123457
Extra info  : b'Indonesian Rupiah' b'Rp'
As currency : JPY : b'\xc2\xa5123,457'
As value    : JPY : 123457
Extra info  : b'Japanese Yen' b'\xc2\xa5'
As currency : JPY : b'-\xc2\xa5123,457'
As value    : JPY : -123457
Extra info  : b'Japanese Yen' b'\xc2\xa5'
As currency : CNY : b'CN\xc2\xa5123,456.79'
As value    : CNY : 123456.79
Extra info  : b'Chinese Yuan' b'CN\xc2\xa5'
As currency : CNY : b'-CN\xc2\xa5123,456.78'
As value    : CNY : -123456.78
Extra info  : b'Chinese Yuan' b'CN\xc2\xa5'

而且它仍然可以在不同的语言环境中工作(巴西以逗号作为小数点而著称):

$ export LC_ALL=pt_BR
$ ./cur.py 
locale: pt_BR
As currency : USD : b'US$123.456,79'
As value    : USD : 123456.79
Extra info  : b'D\xc3\xb3lar americano' b'US$'
As currency : PLN : b'-PLN123.456,78'
As value    : PLN : -123456.78
Extra info  : b'Zloti polon\xc3\xaas' b'PLN'
As currency : PLN : b'PLN123.456,79'
As value    : PLN : 123456.79
Extra info  : b'Zloti polon\xc3\xaas' b'PLN'
As currency : IDR : b'IDR123.457'
As value    : IDR : 123457
Extra info  : b'Rupia indon\xc3\xa9sia' b'IDR'
As currency : JPY : b'JP\xc2\xa5123.457'
As value    : JPY : 123457
Extra info  : b'Iene japon\xc3\xaas' b'JP\xc2\xa5'
As currency : JPY : b'-JP\xc2\xa5123.457'
As value    : JPY : -123457
Extra info  : b'Iene japon\xc3\xaas' b'JP\xc2\xa5'
As currency : CNY : b'CN\xc2\xa5123.456,79'
As value    : CNY : 123456.79
Extra info  : b'Yuan chin\xc3\xaas' b'CN\xc2\xa5'
As currency : CNY : b'-CN\xc2\xa5123.456,78'
As value    : CNY : -123456.78
Extra info  : b'Yuan chin\xc3\xaas' b'CN\xc2\xa5'

值得指出的是babel存在一些编码问题。这是因为(中的locale- data)区域设置文件本身确实使用了不同的编码。如果您使用的是货币,那么应该不会有问题。但是,如果您尝试使用不熟悉的货币,则可能会遇到问题(我刚刚了解到波兰使用iso-8859-2,而不是iso-8859-1)。

python 2022/1/1 18:30:59 有197人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶