python时间戳转换为字符串时候,字符串中存在中文,则会抛出此异常
time_now = int(time.time())
time_time = time.strftime(’%Y%m%d%H%M%S’, time.localtime(time_now))
print(time_time)
异常:UnicodeEncodeError: ‘locale’ codec can’t encode character ‘\u5e74’ in position 2: Illegal byte seque
解决方法:
time_now = 1566284145
time_time = time.strftime( ‘%Y{y}%m{m}%d{d} %H:%M’, time.localtime(time_now)).format(y=‘年’, m=‘月’, d=‘日’)
print(time_time)
注:时间字符串后面如果需要继续加时分秒,在后面自己格式化字符串即可