python 格式化时间含中文报错 ‘locale’ codec can’t encode character ‘\u5e74’ in position 1: Illegal byte sequence

  • A+
所属分类:Python 技术杂谈

date_time = time.strftime('%年-%月-%日 %时:%分:%秒', time.localtime())

运行上面代码会报错:

    date_time = time.strftime('%年-%月-%日 %时:%分:%秒', time.localtime())
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 1: Illegal byte sequence

解决方法:

date_time = time.strftime('%Y{y}-%m{m}-%d{d} %H{h}:%M{i}:%S{s}',time.localtime()).format(y='年',m='月',d='日',h='时',i='分',s='秒')
#运行结果:2018年-11月-19日 20时:25分:08秒

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: