我使用窗口的命令提示符运行了以下 .py 文件。
def main():
import calendar
from datetime import date
import time
# Converting 'datetime.date object to a string'
today=str(date.today())
# Generating a list of string like ['2021', '02', '02']
today=today.split("-")
#Converting string to integer
year=int(today[0])
month=int(today[1])
date=int(today[2])
print(calendar.month(year,month))
if __name__ == "__main__":
main()
它给了我以下输出。
如果我想在 Windows 命令提示符中突出显示今天的日期,如下所示,我需要做什么?
最佳答案
您可以申请Console Virtual Terminal Sequences ,例如如下:
from datetime import date
import calendar
import time
import re
today = date.today()
year = today.year
month = today.month
thism = calendar.month(year,month) # current month
date = today.day.__str__().rjust(2)
rday = ('\\b' + date + '\\b').replace('\\b ', '\\s')
rdayc = "\033[7m" + date + "\033[0m"
# 7 Swaps foreground and background colors
print( re.sub(rday,rdayc,thism))
无论是在 conhost.exe
下运行还是从 Windows 终端运行,都可以在 cmd
、powershell
和 pwsh
上运行:
关于python - 在命令提示符/Windows powershell中使用calendar.py在python中突出显示今天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66021616/