python - 在命令提示符/Windows powershell中使用calendar.py在python中突出显示今天的日期

标签 python powershell dictionary cmd calendar

我使用窗口的命令提示符运行了以下 .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()

它给了我以下输出。

enter image description here

如果我想在 Windows 命令提示符中突出显示今天的日期,如下所示,我需要做什么?

enter image description here

最佳答案

您可以申请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 终端运行,都可以在 cmdpowershellpwsh 上运行:

enter image description here

关于python - 在命令提示符/Windows powershell中使用calendar.py在python中突出显示今天的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66021616/

相关文章:

python - 为什么从字典派生的类不显示包含键

powershell - 所需状态配置与 Azure ARM - 它们是否互补?

json - 从Invoke-Command返回的无关数据

java - 如何从 List<Map<String, Object>> 中删除键/值对

python - 为什么 Python 3 中实例的 __dict__ 大小如此之小?

dictionary - 有没有更惯用的方式在 Kotlin 中初始化这个 map ?

python - Python 3 中的模式匹配字典

python - 基于 Webrequest 在 Airflow 上运行作业

Python:如何访问列表中具有特定键值的字典

Facebook 通过 PowerShell 实现自动化?