如何在命令行中显示英文句子中的当前内容?我在下面的代码中以数字显示。
m = datetime.datetime.now().strftime('%I %M %S')
print 'The time is ' + str(int(m[0:2])) + ' ' + str(int(m[3:5])))
我想看到结果的例子是:
答案 0 :(得分:2)
您可以使用: http://www.daniweb.com/software-development/python/code/216839/number-to-word-converter-python
它将整数转换为英语单词。适应您的需求
答案 1 :(得分: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")
底部的几行是您建议的测试用例。输出如下:
时间是六点(哦)三点。
时间是晚上七点四十五分。
时间是晚上六点(哦)。
时间是晚上十二点。