python格式化时间报错:UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence

执行下列代码:

from datetime import datetime
t = datetime.now()
print(t)
print(t.strftime("%Y年%m月%d日,%H小时%M分钟%S秒")) 

执行上述代码会报错:

 解决方法:

把print(t.strftime("%Y年%m月%d日,%H小时%M分钟%S秒"))改成:

print(t.strftime('%Y{y}%m{m}%d{d} %H{h}%M{f}%S{s}').format(y='年', m='月', d='日', h='时', f='分', s='秒')

 参考网上资料,问题得以解决,记录一下。

posted on 2022-04-04 20:19  我不想一直当菜鸟  阅读(503)  评论(0编辑  收藏  举报