import time
print(time.strftime('%Y年%m月%d日 %H:%M:%S'))
执行上面代码会报错 UnicodeEncodeError: ‘locale’ codec can’t encode character ‘\u5e74’ …
格式文字中把“年月日”汉字去掉则不会报错。
解决办法:用format输入汉字
time.strftime('%Y{y}%m{m}%d{d} %H{h}%M{f}%S{s}').format(y='年',m='月',d='日',h='时',f='分',s='秒')
或
time.strftime('%Y{}%m{}%d{} %H{}%M{}%S{}').format('年','月','日','时','分','秒')
或
time.strftime('%Y{0}%m{1}%d{2} %H{3}%M{4}%S{5}').format('年','月','日','时','分','秒')