python datetime.strftime 不支持日语? [英] python datetime.strftime does not support japanese?
本文介绍了python datetime.strftime 不支持日语?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我遇到以下代码导致编码错误的问题:将包含日语的字符串传递给 strftime 时会发生此问题.
I have a problem that following code causes a encoding error: This problem happens when pass a japanese including string to strftime.
这是由 cmd.exe 上的 python repl 引起的.
这个问题是python的bug吗?
This is caused on python repl on cmd.exe.
Is this problem a bug of python?
import datetime
d = datetime.datetime.now()
d.strftime("%y年")
错误信息:
UnicodeEncodeError: 'locale' codec can't encode character '\u5e74' in position 2: Illegal byte sequence
Python 3.6.5
视窗10 1803
Python 3.6.5
Windows10 1803
推荐答案
在 Python 3.6+ 中,使用 f-string:
In Python 3.6+, using f-string:
import datetime
d = datetime.datetime.now()
y = d.strftime("%y")
assert f'{y}年' == '18年'
这篇关于python datetime.strftime 不支持日语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文