gpt4 book ai didi

__unicode__ 的 Python 字符串格式字符?

转载 作者:太空狗 更新时间:2023-10-30 01:10:30 24 4
gpt4 key购买 nike

首先,有没有?

如果没有,是否有一种很好的方法来强制执行类似的操作

print '%s' % obj

要调用 obj.__unicode__ 而不是 obj.__str__

最佳答案

只需使用 unicode 格式的字符串,而不是在该角色中使用字节字符串:

>>> class X(object):
... def __str__(self): return 'str'
... def __unicode__(self): return u'unicode'
...
>>> x = X()
>>> print u'%s' % x
unicode

关于__unicode__ 的 Python 字符串格式字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3107235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com