如何在python中将数字字转换为数字
我想把用文字表示的数字转换成数字 比如说,,如何在python中将数字字转换为数字,python,nlp,fuzzy,Python,Nlp,Fuzzy,我想把用文字表示的数字转换成数字 比如说,, 三万四千四百五十转换为相应的数值34450。 还有一些模糊转换,如“请支付三万四千四百五十美元”然后输出为34450对于从数字到单词的转换,请尝试“num2words”软件包: 对于words to num,我从这里的代码稍微调整了代码: ->五十亿贰拾贰万贰仟贰佰贰拾叁元贰角捌分 ->50002223.28要将数字转换为单词,请尝试“num2words”软件包: 对于words to num,我从这里的代码稍微调整了代码: ->五十亿贰拾贰
三万四千四百五十
转换为相应的数值34450
。
还有一些模糊转换,如“请支付三万四千四百五十美元”然后输出为34450
对于从数字到单词的转换,请尝试“num2words”软件包:
对于words to num,我从这里的代码稍微调整了代码:
->五十亿贰拾贰万贰仟贰佰贰拾叁元贰角捌分
->50002223.28要将数字转换为单词,请尝试“num2words”软件包:
对于words to num,我从这里的代码稍微调整了代码:
->五十亿贰拾贰万贰仟贰佰贰拾叁元贰角捌分
->50002223.28我要求的是单词到数字,而不是数字到单词我已经更新了答案我认为它无法转换小数点@Oxymoron88你能举一个例子说明单词在小数点处的外观吗?如果它像“十一点六三”,你只需要把字符串和点分开,把单词和数字分开,用小数点的数字把它们连在一起。它从来没有说成十一点六三,而是像十一点六三,那么塞纳里奥会是怎样的呢,不是数字到单词我已经更新了答案我不认为它能够转换小数点@Oxymoron88你能举个例子说明单词在小数点处的外观吗?如果它像“十一点六三”,你只需要把字符串和点分开,把单词和数字分开,用小数点将它们连在一起。它从来没有说成十一点六三,而是像十一点六三,senario会是怎样的呢?这是一个愚蠢的实现:=)这是一个愚蠢的实现:=)
from num2words import num2words
def text2int(textnum, numwords={}):
if not numwords:
units = [
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
"sixteen", "seventeen", "eighteen", "nineteen",
]
tens = ["", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"]
scales = ["hundred", "thousand", "million", "billion", "trillion"]
numwords["and"] = (1, 0)
for idx, word in enumerate(units): numwords[word] = (1, idx)
for idx, word in enumerate(tens): numwords[word] = (1, idx * 10)
for idx, word in enumerate(scales): numwords[word] = (10 ** (idx * 3 or 2), 0)
current = result = 0
for word in textnum.split():
if word not in numwords:
raise Exception("Illegal word: " + word)
scale, increment = numwords[word]
current = current * scale + increment
if scale > 100:
result += current
current = 0
return result + current
#### My update to incorporate decimals
num = 5000222223.28
fullText = num2words(num).replace('-',' ').replace(',',' ')
print fullText
decimalSplit = fullText.split('point ')
if len(decimalSplit) > 1:
decimalSplit2 = decimalSplit[1].split(' ')
decPart = sum([float(text2int(decimalSplit2[x]))/(10)**(x+1) for x in range(len(decimalSplit2))])
else:
decPart = 0
intPart = float(text2int(decimalSplit[0]))
Value = intPart + decPart
print Value