0

如何在命令行中显示当前的英文句子?我在数字显示下方有以下代码。

m = datetime.datetime.now().strftime('%I %M %S')
print 'The time is ' + str(int(m[0:2])) + ' ' + str(int(m[3:5]))) 

我想看到结果的例子是:

  • 时间是晚上七点四十五分
  • 时间是晚上十一点(哦)六点
  • 时间是上午十二点
4

2 回答 2

2

你可以使用它: http ://www.daniweb.com/software-development/python/code/216839/number-to-word-converter-python

它将整数转换为英文单词。使其适应您的需求

于 2013-07-22T15:56:45.590 回答
1

这是我根据您的要求改编的脚本:

import datetime as dt

d = {0: "(oh)",
     1: "one",
     2: "two",
     3: "three",
     4: "four",
     5: "five",
     6: "six",
     7: "seven",
     8: "eight",
     9: "nine",
     10: "ten",
     11: "eleven",
     12: "twelve",
     13: "thirteen",
     14: "fourteen",
     15: "fifteen",
     16: "sixteen",
     17: "seventeen",
     18: "eighteen",
     19: "nineteen",
     20: "twenty",
     30: "thirty",
     40: "forty",
     50: "fifty",
     60: "sixty"}

def display_time(t):
    Hour = d[int( t[0:2])] if t[0:2] != "00" else d[12]
    Suffix = 'a.m.' if d[int( t[7:9])] == Hour else 'p.m.'

    if  t[3] == "0":
        if  t[4] == "0":
            Minute = ""
        else:
            Minute = d[0] + " " + d[int(t[4])]
    else:
        Minute = d[int(t[3])*10] + '-' + d[int(t[4])]
    print 'The time is', Hour, Minute, Suffix

display_time(dt.datetime.now().strftime('%I %M %H'))
display_time("07 45 19")
display_time("11 06 23")
display_time("00 00 00")

底部的几行是您建议的测试用例。输出如下:

时间是下午六点(哦)三点

时间是晚上七点四十五分

时间是晚上十一点(哦)六点

时间是下午十二点

于 2013-07-22T17:04:44.637 回答