python实现将英文单词表示的数字转换成阿拉伯数字的方法

   本文实例讲述了python实现将英文单词表示的数字转换成阿拉伯数字的方法。分享给大家供大家参考。具体实现方法如下:

  ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

import re
_known = {
'zero': 0,
'one': 1,
'two': 2,
'three': 3,
'four': 4,
'five': 5,
'six': 6,
'seven': 7,
'eight': 8,
'nine': 9,
'ten': 10,
'eleven': 11,
'twelve': 12,
'thirteen': 13,
'fourteen': 14,
'fifteen': 15,
'sixteen': 16,
'seventeen': 17,
'eighteen': 18,
'nineteen': 19,
'twenty': 20,
'thirty': 30,
'forty': 40,
'fifty': 50,
'sixty': 60,
'seventy': 70,
'eighty': 80,
'ninety': 90
}
def spoken_word_to_number(n):
"""Assume n is a positive integer".
assert _positive_integer_number('nine hundred') == 900
assert spoken_word_to_number('one hundred') == 100
assert spoken_word_to_number('eleven') == 11
assert spoken_word_to_number('twenty two') == 22
assert spoken_word_to_number('thirty-two') == 32
assert spoken_word_to_number('forty two') == 42
assert spoken_word_to_number('two hundred thirty two') == 232
assert spoken_word_to_number('two thirty two') == 232
assert spoken_word_to_number('nineteen hundred eighty nine') == 1989
assert spoken_word_to_number('nineteen eighty nine') == 1989
assert spoken_word_to_number('one thousand nine hundred and eighty nine') == 1989
assert spoken_word_to_number('nine eighty') == 980
assert spoken_word_to_number('nine two') == 92 # wont be able to convert this one
assert spoken_word_to_number('nine thousand nine hundred') == 9900
assert spoken_word_to_number('one thousand nine hundred one') == 1901
"""
n = n.lower().strip()
if n in _known:
return _known[n]
else:
inputWordArr = re.split('[ -]', n)
assert len(inputWordArr) > 1 #all single words are known
#Check the pathological case where hundred is at the end or thousand is at end
if inputWordArr[-1] == 'hundred':
inputWordArr.append('zero')
inputWordArr.append('zero')
if inputWordArr[-1] == 'thousand':
inputWordArr.append('zero')
inputWordArr.append('zero')
inputWordArr.append('zero')
if inputWordArr[0] == 'hundred':
inputWordArr.insert(0, 'one')
if inputWordArr[0] == 'thousand':
inputWordArr.insert(0, 'one')
inputWordArr = [word for word in inputWordArr if word not in ['and', 'minus', 'negative']]
currentPosition = 'unit'
prevPosition = None
output = 0
for word in reversed(inputWordArr):
if currentPosition == 'unit':
number = _known[word]
output += number
if number > 9:
currentPosition = 'hundred'
else:
currentPosition = 'ten'
elif currentPosition == 'ten':
if word != 'hundred':
number = _known[word]
if number < 10:
output += number*10
else:
output += number
#else: nothing special
currentPosition = 'hundred'
elif currentPosition == 'hundred':
if word not in [ 'hundred', 'thousand']:
number = _known[word]
output += number*100
currentPosition = 'thousand'
elif word == 'thousand':
currentPosition = 'thousand'
else:
currentPosition = 'hundred'
elif currentPosition == 'thousand':
assert word != 'hundred'
if word != 'thousand':
number = _known[word]
output += number*1000
else:
assert "Can't be here" == None
return(output)

  希望本文所述对大家的Python程序设计有所帮助。

时间: 2024-09-15 07:14:12

python实现将英文单词表示的数字转换成阿拉伯数字的方法的相关文章

Python实现把数字转换成中文

  这篇文章主要介绍了Python实现把数字转换成中文,一般用于数字金额转中文大写金额,即将阿拉伯数字转换为大写的中文,需要的朋友可以参考下 周末在家,写了个小程序,用于将阿拉伯数字转换化大写中文.程序没经过任何优化,出没经过详细的测试,挂到网上,方便将来有需要的时候直接拿来用. ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

python实现将元祖转换成数组的方法

  这篇文章主要介绍了python实现将元祖转换成数组的方法,涉及Python中list方法的使用技巧,需要的朋友可以参考下 本文实例讲述了python实现将元祖转换成数组的方法.分享给大家供大家参考.具体分析如下: python的元祖使用一对小括号表示的,元素是固定的,如果希望添加新的元素,可以先将元祖转换成数组列表,再进行操作 ? 1 2 3 colour_tuple = ("Red","Green","Blue") colour_list

python将ip地址转换成整数的方法

 这篇文章主要介绍了python将ip地址转换成整数的方法,涉及Python针对IP地址的转换技巧,需要的朋友可以参考下     本文实例讲述了python将ip地址转换成整数的方法.分享给大家供大家参考.具体分析如下: 有时候我们用数据库存储ip地址时可以将ip地址转换成整数存储,整数占用空间小,索引也会比较方便,下面的python代码自定义了一个ip转换成整数的函数,非常简单,代码同时还提供了整数转换成ip地址的方法. ? 1 2 3 4 5 6 7 import socket, struc

JavaScript实现把数字转换成中文

  这篇文章主要介绍了JavaScript实现把数字转换成中文,本文直接给出实例代码,需要的朋友可以参考下 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

SQL SERVER函数将人民币数字转换成大写形式

  SQL SERVER函数将人民币数字转换成大写形式 CREATE FUNCTION [dbo].[f_num_chn] (@num numeric(14,5)) RETURNS varchar(100) WITH ENCRYPTION AS BEGIN DECLARE @n_data VARCHAR(20),@c_data VARCHAR(100),@n_str VARCHAR(10),@i int SET @n_data=RIGHT(SPACE(14)+CAST(CAST(ABS(@num

python实现将文本转换成语音的方法

  本文实例讲述了python将文本转换成语音的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70

python使用reportlab实现图片转换成pdf的方法

  本文实例讲述了python使用reportlab实现图片转换成pdf的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/usr/bin/env python import os import sys from reportlab.lib.pagesizes import A4, landscape from reportlab.pdfgen import canvas f = sys.argv[1]

JavaScript将数字转换成大写中文的方法

 这篇文章主要介绍了JavaScript将数字转换成大写中文的方法,涉及javascript字符串及匹配的技巧,具有一定参考借鉴价值,需要的朋友可以参考下     本文实例讲述了JavaScript将数字转换成大写中文的方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function intToChinese ( str ) { str

js字符串转换成数字与数字转换成字符串的实现方法

 本篇文章主要是对js字符串转换成数字与数字转换成字符串的实现方法进行了详细的介绍,需要的朋友可以过来参考下,希望对大家有所帮助 js字符串转换成数字   将字符串转换成数字,得用到parseInt函数. parseInt(string) : 函数从string的开始解析,返回一个整数.     举例: parseInt('123') : 返回 123(int): parseInt('1234xxx') : 返回 1234(int):   如果解析不到数字,则将返回一个NaN的值,可以用isNa