当前位置: 首页 > 面试题库 >

在Python中将货币解析为数字

章光华
2023-03-14
问题内容

我刚刚从Python中将数字格式设置为货币中得知,Python模块babel提供了babel.numbers.format_currency将数字格式设置为货币。例如,

from babel.numbers import format_currency

s = format_currency(123456.789, 'USD', locale='en_US')  # u'$123,456.79'
s = format_currency(123456.789, 'EUR', locale='fr_FR')  # u'123\xa0456,79\xa0\u20ac'

从货币到数字(例如$123,456,789.00->)的反向效果如何123456789babel提供babel.numbers.parse_number解析本地数字的功能,但是我没有找到parse_currency。那么,将本地货币解析为数字的理想方法是什么?

我经历了Python:从string中除去数字以外的字符。

# Way 1
import string
all=string.maketrans('','')
nodigs=all.translate(all, string.digits)

s = '$123,456.79'
n = s.translate(all, nodigs)    # 12345679, lost `.`

# Way 2
import re
n = re.sub("\D", "", s)         # 12345679

不用担心小数点分隔符.

.从字符串中删除除以外的所有非数字字符(请参阅此处),

import re

# Way 1:
s = '$123,456.79'
n = re.sub("[^0-9|.]", "", s)   # 123456.79

# Way 2:
non_decimal = re.compile(r'[^\d.]+')
s = '$123,456.79'
n = non_decimal.sub('', s)      # 123456.79

它确实处理小数点分隔符.

但是,例如,上述解决方案不起作用

from babel.numbers import format_currency
s = format_currency(123456.789, 'EUR', locale='fr_FR')  # u'123\xa0456,79\xa0\u20ac'
new_s = s.encode('utf-8') # 123 456,79 €

如您所见,货币格式各不相同。 一般将货币解析为数字的理想方法是什么?


问题答案:

使用通天塔

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)。



 类似资料:
  • 问题内容: 假设我提供了一个类似于或类似此字符串的字符串(带有数字和小数点以及一些分隔符,例如或或其他不可预测的字符串)。我需要编写一个方法,该方法接受像上面的参数一样的参数,并分别返回像或的String 。 您能否建议最有效,最易读的代码来实现这一目标? 注意:我知道要遍历每个索引,并检查它的重调谐器是否存在,或者 我正在效率和可读性方面寻找更优化的解决方案 谢谢。 问题答案: 这将删除任何不是

  • 问题内容: 我在字符串变量中有美元 我如何将其转换为小数而不是字符串,以便可以对其进行操作,例如向其中添加美元? 问题答案: 有一个简单的方法:

  • 问题内容: 我想使用Python将类似188518982.18的数字格式化为188,518,982.18英镑。 我怎样才能做到这一点? 问题答案: 请参阅语言环境模块。 这会进行货币(和日期)格式化。

  • 问题内容: 有没有办法让这样的字符串的浮点值:,比其他? 我希望这取决于当前的网站语言,有时逗号可以用点代替。 问题答案: 您可以使用 -解析货币编号 手册中的示例: 给出:

  • 问题内容: 有没有一种方法可以将int转换为货币格式。我可以连接“ $”符号,但是如何将放入其中呢?像将int = 1000变成$ 1,000一样? 还是有一个功能可以完成所有工作?是否有字符串替换? 问题答案: FORMAT()函数:http : //dev.mysql.com/doc/refman/5.5/zh- CN/string- functions.html#function_forma

  • 在我们继续之前,我们需要先来知道“菲亚特”或“菲亚特货币”的含义。 菲亚特货币由政府宣布为其控制的领土内的法定货币。仅因政府监管或法律而具有价值的菲亚特货币不受黄金或白银等实物商品的支持。法定货币的价值来自供需关系和发行政府的稳定性,而不是支持它的商品的价值。它基于经济的信念和信誉。大多数现代纸币都是法定货币。 每个加密货币的新手都想知道如何兑现比特币或从比特币中提取到法定货币(美元,欧元,印度卢