0

I have a string which has numbers in words format(with a string prefix). I want to convert it to string with numbers in integer format(string prefix remains as it is ). Here are some sample data of string:

"m.l. one two three four"
"k.f two seven six eight"
"g.h three nine zero four"

What I want each of them to converted to:

"ML1234"
"KF2768"
"GH3904"

I looked around on SO but couldn't find a relevant question(most of the SO questions were related to converting tens,hundreds,thousands to integers).

How can I convert this?

user2966197
  • 2,793
  • 10
  • 45
  • 77
  • Check this out: https://pypi.python.org/pypi/words2num – perigon Aug 10 '17 at 03:50
  • See also: https://stackoverflow.com/questions/493174/is-there-a-way-to-convert-number-words-to-integers – perigon Aug 10 '17 at 03:52
  • @perigon I had looked up at the SO post you mentioned but it is converting string like "seven billion three thousand..." into numbers. My requirement is to take individual words and convert it to number. So `m.l. two seven six eight` becomes `ML2768` – user2966197 Aug 10 '17 at 03:56

4 Answers4

3

A simple solution:

string = "m.l. one two three four"

text_dict = {'one':1, 'two':2, 'three':3, 'four':4, 'five':5, 'six': 6, 'seven':7, 'eight':8, 'nine':9, 'zero':0}
split = string.split()
numerized = [str(text_dict[word]) for word in split[1:]]
prefix = split[0].upper().replace(".","")
print ("".join([prefix] + numerized))

Output:

ML1234
perigon
  • 2,160
  • 11
  • 16
0

A solution with functools.reduce:

from functools import reduce

text = "m.l. one two three four"

rep = [(" zero", "0"), (" one", "1"), (" two", "2"), (" three", "3"),
       (" four", "4"), (" five", "5"), (" six", "6"), (" seven", "7"),
       (" eight", "8"), (" nine", "9"), (".", "")]
t = reduce(lambda a, kv: a.replace(*kv), rep, text.lower()).upper()
print(t)

Outputs:

ML1234
umutto
  • 7,460
  • 4
  • 43
  • 53
0
data = "m.l. one two three four"
numbers = {
    "one":1,
    "two":2,
    "three":3,
    "four":4,
    "five":5,
    "six":6,
    "seven":7,
    "eight":8,
    "nine":9,
    "zero":0
}
def num_map(str):
    return numbers[str]
data = data.split()
# data[0] will be having chars, rest are numbers

tmp =  ''.join(data[0].split('.')[:-1])
tmp += ''.join(map(str,map(num_map, data[1:])))
print tmp.upper()

map string to int and vice versa

not_python
  • 904
  • 6
  • 13
0

You have so many options to choose, apart from that you have to use reduce function or a small library called words2num.

If you are using python 2.x you can include directly reduce, or if you are using python 3.x use functools.reduce.

From umutto's answer: Python 2.x

text = "m.l. one two three four"

rep = [(" zero", "0"), (" one", "1"), (" two", "2"), (" three", "3"),
   (" four", "4"), (" five", "5"), (" six", "6"), (" seven", "7"),
   (" eight", "8"), (" nine", "9"), (".", "")]
t = reduce(lambda a, kv: a.replace(*kv), rep, text.lower()).upper()
print t

Output

ML1234

This is enough. If you want to use a library words2num is a good option

1, Install words2num

pip install words2num

2, Import 'words2num'

from words2num import w2n

3, Create code with w2n() function

string = "m.l. one two three four"

word_str = string.split(" ")
result = ""

for i in word_str:
    try:
            result += str(w2n(i))
    except:
            words = i.upper().split(".")
            result += "".join(words)
print result

Output

ML1234

Arun V Jose
  • 3,321
  • 2
  • 24
  • 24
  • reduce and functools.reduce are the same thing. Its just moved under functools library in [Python 3.x](https://docs.python.org/3.0/whatsnew/3.0.html). – umutto Aug 10 '17 at 06:02
  • 1
    I am sorry about that I just use only 2.x. well thank you for that information. – Arun V Jose Aug 10 '17 at 10:12