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