python中numstr,Python中灵活的数字字符串解析

Are there any Python libraries that help parse and validate numeric strings beyond what is supported by the built-in float() function? For example, in addition to simple numbers (1234.56) and scientific notation (3.2e15), I would like to be able to parse formats like:

Numbers with commas: 2,147,483,647

Named large numbers: 5.5 billion

Fractions: 1/4

I did a bit of searching and could not find anything, though I would be surprised if such a library did not already exist.

解决方案

If you want to convert "localized" numbers such as the American "2,147,483,647" form, you can use the atof() function from the locale module. Example:

import locale

locale.setlocale(locale.LC_NUMERIC, 'en_US')

print locale.atof('1,234,456.23') # Prints 1234456.23

As for fractions, Python now handles them directly (since version 2.6); they can even be built from a string:

from fractions import Fraction

x = Fraction('1/4')

print float(x) # 0.25

Thus, you can parse a number written in any of the first 3 ways you mention, only with the help of the above two standard modules:

try:

num = float(num_str)

except ValueError:

try:

num = locale.atof(num_str)

except ValueError:

try:

num = float(Fraction(num_str))

except ValueError:

raise Exception("Cannot parse '%s'" % num_str) # Or handle '42 billion' here

# 'num' has the numerical value of 'num_str', here.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值