time.strftime:格式化字符串中含中文報錯處理
阿新 • • 發佈:2019-01-30
port 格式化輸出 cal 結果 報錯 hour error dst ror
import time print(time.strftime("%Y年%m月%d日 %H時%M分%S秒",time.localtime()))
報錯:
UnicodeEncodeError: ‘locale‘ codec can‘t encode character ‘\u5e74‘ in position 2: Illegal byte sequence
打印當前時間
import time print(time.localtime())
運行結果:
time.struct_time(tm_year=2019, tm_mon=1, tm_mday=30, tm_hour=11, tm_min=22, tm_sec=48, tm_wday=2, tm_yday=30, tm_isdst=0)
格式化輸出
import time print(time.strftime("%Y{}%m{}%d{} %H{}%M{}%S{}",time.localtime()).format(‘年‘,‘月‘,‘日‘,‘時‘,‘分‘,‘秒‘))
運行結果:
2019年01月30日 11時22分48秒
為什麽不能直接寫成字符串,沒有理解。
time.strftime:格式化字符串中含中文報錯處理