python 格式化時間含中文報錯 UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence'


import time

print(time.strftime(u'%Y年%m月%d日',time.localtime(time.time())))

執行上面代碼會報錯  UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence


解決方式:
time.strftime('%Y{y}%m{m}%d{d}').format(y='年',m='月',d='日')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM