使用python语言实现获取当前时间,按照电子表样式打印到控制台

使用python语言实现获取当前时间,按照电子表样式打印到控制台

clock.py

  1 from datetime import datetime
  2 
  3 # 数字0
  4 zero_line_01 = '  ---  '
  5 zero_line_02 = ' |   | '
  6 zero_line_03 = ' |   | '
  7 zero_line_04 = ' |   | '
  8 zero_line_05 = '  ___  '
  9 
 10 # 数字1
 11 one_line_01 = '   |   '
 12 one_line_02 = '   |   '
 13 one_line_03 = '   |   '
 14 one_line_04 = '   |   '
 15 one_line_05 = '   |   '
 16 
 17 # 数字2
 18 two_line_01 = '  ---  '
 19 two_line_02 = '     | '
 20 two_line_03 = '  ---  '
 21 two_line_04 = ' |     '
 22 two_line_05 = '  ---  '
 23 
 24 # 数字3
 25 three_line_01 = '  ---  '
 26 three_line_02 = '     | '
 27 three_line_03 = '  ---  '
 28 three_line_04 = '     | '
 29 three_line_05 = '  ---  '
 30 
 31 # 数字4
 32 four_line_01 = ' |   | '
 33 four_line_02 = ' |   | '
 34 four_line_03 = '  ---  '
 35 four_line_04 = '     | '
 36 four_line_05 = '     | '
 37 
 38 # 数字5
 39 five_line_01 = '  ---  '
 40 five_line_02 = ' |     '
 41 five_line_03 = '  ---  '
 42 five_line_04 = '     | '
 43 five_line_05 = '  ---  '
 44 
 45 # 数字6
 46 six_line_01 = '  ---  '
 47 six_line_02 = ' |     '
 48 six_line_03 = '  ---  '
 49 six_line_04 = ' |   | '
 50 six_line_05 = '  ---  '
 51 
 52 # 数字7
 53 seven_line_01 = '  ---  '
 54 seven_line_02 = '     | '
 55 seven_line_03 = '     | '
 56 seven_line_04 = '     | '
 57 seven_line_05 = '     | '
 58 
 59 # 数字8
 60 eight_line_01 = '  ---  '
 61 eight_line_02 = ' |   | '
 62 eight_line_03 = '  ---  '
 63 eight_line_04 = ' |   | '
 64 eight_line_05 = '  ---  '
 65 
 66 # 数字9
 67 nine_line_01 = '  ---  '
 68 nine_line_02 = ' |   | '
 69 nine_line_03 = '  ---  '
 70 nine_line_04 = '     | '
 71 nine_line_05 = '  ---  '
 72 
 73 # 分隔符
 74 separator_line_01 = '       '
 75 separator_line_02 = '   .   '
 76 separator_line_03 = '       '
 77 separator_line_04 = '   .   '
 78 separator_line_05 = '       '
 79 
 80 # 分隔符
 81 empty_line_01 = '       '
 82 empty_line_02 = '       '
 83 empty_line_03 = '       '
 84 empty_line_04 = '       '
 85 empty_line_05 = '       '
 86 
 87 # 获取当前时间字符串
 88 def get_current_time_str():
 89     return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
 90 
 91 # 根据字符串返返回电子表格式内容
 92 def generate_content_by_datetime_str(datetime_str):
 93     result = {
 94         'result_line_01' : '',
 95         'result_line_02' : '',
 96         'result_line_03' : '',
 97         'result_line_04' : '',
 98         'result_line_05' : ''
 99     }
100     char_list = list(datetime_str)
101     for char in char_list:
102         clock_result = generate_clock(char)
103         result['result_line_01'] += clock_result['clock_result_01']
104         result['result_line_02'] += clock_result['clock_result_02']
105         result['result_line_03'] += clock_result['clock_result_03']
106         result['result_line_04'] += clock_result['clock_result_04']
107         result['result_line_05'] += clock_result['clock_result_05']
108     return result
109 
110 # 根据当前时间返回电子表格式内容
111 def generate_content():
112     datetime_str = get_current_time_str()
113     result = generate_content_by_datetime_str(datetime_str)
114     return result
115 
116 def generate_clock(str):
117     clock_result = {'clock_result_01' : '',
118                     'clock_result_02' : '',
119                     'clock_result_03' : '',
120                     'clock_result_04' : '',
121                     'clock_result_05' : ''}
122     if '0' == str:
123         clock_result['clock_result_01'] = zero_line_01
124         clock_result['clock_result_02'] = zero_line_02
125         clock_result['clock_result_03'] = zero_line_03
126         clock_result['clock_result_04'] = zero_line_04
127         clock_result['clock_result_05'] = zero_line_05
128     if '1' == str:
129         clock_result['clock_result_01'] = one_line_01
130         clock_result['clock_result_02'] = one_line_02
131         clock_result['clock_result_03'] = one_line_03
132         clock_result['clock_result_04'] = one_line_04
133         clock_result['clock_result_05'] = one_line_05
134     if '2' == str:
135         clock_result['clock_result_01'] = two_line_01
136         clock_result['clock_result_02'] = two_line_02
137         clock_result['clock_result_03'] = two_line_03
138         clock_result['clock_result_04'] = two_line_04
139         clock_result['clock_result_05'] = two_line_05
140     if '3' == str:
141         clock_result['clock_result_01'] = three_line_01
142         clock_result['clock_result_02'] = three_line_02
143         clock_result['clock_result_03'] = three_line_03
144         clock_result['clock_result_04'] = three_line_04
145         clock_result['clock_result_05'] = three_line_05
146     if '4' == str:
147         clock_result['clock_result_01'] = four_line_01
148         clock_result['clock_result_02'] = four_line_02
149         clock_result['clock_result_03'] = four_line_03
150         clock_result['clock_result_04'] = four_line_04
151         clock_result['clock_result_05'] = four_line_05
152     if '5' == str:
153         clock_result['clock_result_01'] = five_line_01
154         clock_result['clock_result_02'] = five_line_02
155         clock_result['clock_result_03'] = five_line_03
156         clock_result['clock_result_04'] = five_line_04
157         clock_result['clock_result_05'] = five_line_05
158     if '6' == str:
159         clock_result['clock_result_01'] = six_line_01
160         clock_result['clock_result_02'] = six_line_02
161         clock_result['clock_result_03'] = six_line_03
162         clock_result['clock_result_04'] = six_line_04
163         clock_result['clock_result_05'] = six_line_05
164     if '7' == str:
165         clock_result['clock_result_01'] = seven_line_01
166         clock_result['clock_result_02'] = seven_line_02
167         clock_result['clock_result_03'] = seven_line_03
168         clock_result['clock_result_04'] = seven_line_04
169         clock_result['clock_result_05'] = seven_line_05
170     if '8' == str:
171         clock_result['clock_result_01'] = eight_line_01
172         clock_result['clock_result_02'] = eight_line_02
173         clock_result['clock_result_03'] = eight_line_03
174         clock_result['clock_result_04'] = eight_line_04
175         clock_result['clock_result_05'] = eight_line_05
176     if '9' == str:
177         clock_result['clock_result_01'] = nine_line_01
178         clock_result['clock_result_02'] = nine_line_02
179         clock_result['clock_result_03'] = nine_line_03
180         clock_result['clock_result_04'] = nine_line_04
181         clock_result['clock_result_05'] = nine_line_05
182     if ':' == str:
183         clock_result['clock_result_01'] = separator_line_01
184         clock_result['clock_result_02'] = separator_line_02
185         clock_result['clock_result_03'] = separator_line_03
186         clock_result['clock_result_04'] = separator_line_04
187         clock_result['clock_result_05'] = separator_line_05
188     if ' ' == str:
189         clock_result['clock_result_01'] = empty_line_01
190         clock_result['clock_result_02'] = empty_line_02
191         clock_result['clock_result_03'] = empty_line_03
192         clock_result['clock_result_04'] = empty_line_04
193         clock_result['clock_result_05'] = empty_line_05
194     return clock_result

 

test.py

 1 import clock
 2 
 3 # 测试时钟方法
 4 # clock_result = clock.generate_content_by_datetime_str('1997-05-29 00:00:00')
 5 # print(clock_result['result_line_01'])
 6 # print(clock_result['result_line_02'])
 7 # print(clock_result['result_line_03'])
 8 # print(clock_result['result_line_04'])
 9 # print(clock_result['result_line_05'])
10 
11 while True:
12     clock_result = clock.generate_content()
13     print(clock_result['result_line_01'])
14     print(clock_result['result_line_02'])
15     print(clock_result['result_line_03'])
16     print(clock_result['result_line_04'])
17     print(clock_result['result_line_05'])
18     print("")
19     time.sleep(1)

 实现效果:

 

posted @ 2024-11-16 21:53  尘世间迷茫的小书童  阅读(5)  评论(0编辑  收藏  举报