如何将一个随机的电话号码从一个数字转换成词典,如何从文本中选择它,并在python中转换? [英] How to convert a random telephone number from a number into words from dictionaries and how to select it from a text and convert it in python?

查看:153
本文介绍了如何将一个随机的电话号码从一个数字转换成词典,如何从文本中选择它,并在python中转换?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码将数字中的随机电话号码转换为字词:

I am trying to convert a random telephone number from a number into words using dictionaries using the following code:

dict_1 = {07: 'zero seven'}

dict_2 = {2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight'}

dict_3 = {0: 'zero', 1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five', 6: 'six', 7: 'seven', 8: 'eight', 9: 'nine'}

def main():

    import re
    no_tel = raw_input("Enter a phrase: ")
    mat = re.search('(\d{10})', no_tel)
    a_no = mat.group(0)
    first_part = a_no[0:2]
    second_part = a_no[2:3]
    third_part = a_no[3:]

    it_works = False
    if dict_1.get(int(first_part)) != None:
        a_print = dict_1.get(int(first_part))
        it_works = True
    else:
        print "Error!"

    if dict_2.get(int(second_part)) != None:
        b_print = dict_2.get(int(second_part))
        it_works = True
    else:
        print "Error!"
    fourth_part = third_part[0]
    fifth_part = third_part[1]
    sixth_part = third_part[2]
    seventh_part = third_part[3]
    eighth_part = third_part[4]
    ninth_part = third_part[5]
    tenth_part = third_part[6]

    if dict_3.get(int(fourth_part)) != None:
        d_print = dict_3.get(int(fourth_part))
        it_works = True

    if dict_3.get(int(fifth_part)) != None:
        e_print = dict_3.get(int(fifth_part))
        it_works = True

    if dict_3.get(int(sixth_part)) != None:
        f_print = dict_3.get(int(sixth_part))
        it_works = True

    if dict_3.get(int(seventh_part)) != None:
        g_print = dict_3.get(int(seventh_part))
        it_works = True

    if dict_3.get(int(eighth_part)) != None:
        h_print = dict_3.get(int(eighth_part))
        it_works = True

    if dict_3.get(int(ninth_part)) != None:
        i_print = dict_3.get(int(ninth_part))
        it_works = True


    if dict_3.get(int(tenth_part)) != None:
        j_print = dict_3.get(int(tenth_part))
        it_works = True

    if it_works == True:
        telephone = " ". join([a_print, b_print, d_print, e_print, f_print, g_print, h_print, i_print, j_print])
        correct_no = re.sub ('(first_part)(second_part)(third_part)', telephone, no_tel)
        print correct_no
    else:
        print "Error!"


if __name__ == '__main__':
    main()

运行此程序后,输出应为:

After running this program the output should be:

Enter a phrase: My phone no is 0734123456

上述代码应转换为 - >

which the above code should convert to ->

My phone no is zero seven two three one two three four five six

或此格式的任何其他电话号码。

or any other phone no in this format.

同样,代码如何应用于包含电话号码的文档?
我不完全如何做,从文本中读取,然后用电话号码替换内容显示,我不知道。

Also how can the code be applied to a document containing telephone numbers? I do not exactly how to do it, read from text and then display the content with the telephone numbers substituted, I do not know.

推荐答案

为什么不只是:

def phone(number):
    numbers = ['zero', 'one', 'two', 'three', 'four',
               'five', 'six', 'seven', 'eight', 'nine']
    return ' '.join(numbers[c] for c in map(int, number))

结果:

>>> phone('0734123456')
'zero seven three four one two three four five six'

这篇关于如何将一个随机的电话号码从一个数字转换成词典,如何从文本中选择它,并在python中转换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆