python控制台显示时钟的示例

yipeiwu_com5年前Python基础

复制代码 代码如下:

#!/usr/bin/env python
# coding: utf-8
#
#
# show time in console
#
import sys
import time

raws = '''
.--.

|  |

`--`
  .
 /|

  |
 ---
---.

---`

`---
---.

---|

---`
.  .

`--|

   |
.---

`--.

---`
.---

|--.

`--`
.--.

`  |

   |
.--.

|--|

`--`
.--.

`--|

---`
'''.strip()
numbers = {}
def init():
    for num in range(10):
        numbers[str(num)] = []
    lineno = 0
    for line in raws.split('\n'):
        line = line.ljust(4)
        arr = []
        for char in line:
            arr.append(char) # != ' ')
        numbers[str(lineno/5)].append(arr)
        lineno += 1
    numbers[':'] = [[' ', ' ', ' ', ' '], [' ', ' ', '-', ' '], [' ', ' ', ' ', ' '], [' ', ' ', '-', ' '], [' ', ' ', ' ', ' ']]
    numbers[' '] = [[' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' '], [' ', ' ', ' ', ' ']]
def print_num(digtal):
    digtal = str(digtal)
    screen = []
    for i in range(5):
        screen.append([])
    for num in digtal:
        for i, linechar in enumerate(numbers[num]):
            for char in linechar:
                screen[i].append(char)
            screen[i].append('   ')
    for line in screen:
        print ''.join(line)
init()
def cls():
    sys.stdout.write('\033[2J\033[0;0H')
    sys.stdout.flush()

while True:
    t = time.strftime("%H:%M:%S")
    cls(); print_num(t)
    time.sleep(1)
    t = time.strftime("%H %M %S")
    cls(); print_num(t)
    time.sleep(1)

相关文章

python 实现提取某个索引中某个时间段的数据方法

如下所示: from elasticsearch import Elasticsearch import datetime import time import dateutil.p...

Python去除字符串前后空格的几种方法

其实如果要去除字符串前后的空格很简单,那就是用strip(),简单方便 >>> ' A BC '.strip() 'A BC' 如果不允许用strip()的方法,...

Python这样操作能存储100多万行的xlsx文件

Python这样操作能存储100多万行的xlsx文件

(1) 如果excel文件是xls,2003版的,使用xlrd和xlwt库来对xls文件进行操作 (2) 如果excel文件是xlsx,2007以上版的,使用openpyxl库来对xls...

Python 中 function(#) (X)格式 和 (#)在Python3.*中的注意事项

python 的语法定义和C++、matlab、java 还是很有区别的。 1. 括号与函数调用 def devided_3(x): return x/3. print(a)...

python算法学习之计数排序实例

python算法学习之计数排序实例 复制代码 代码如下:# -*- coding: utf-8 -*- def _counting_sort(A, B, k):  &...