我想在Python3中使用原始字节对象进行sprintf操作,而无需进行任何手动转换以使%s正常工作。 因此,将字节对象作为“模板”,加上任意数量的任何类型的对象,并返回渲染后的字节对象。 这就是Python 2的sprintf%操作符始终起作用的方式。
b'test %s %s %s' % (5, b'blah','strblah') # python3 ==> error
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: %b requires bytes, or an object that implements __bytes__, not 'int'
def to_bytes(arg):
if hasattr(arg,'encode'): return arg.encode()
if hasattr(arg,'decode'): return arg
return repr(arg).encode()
def render_bytes_template(btemplate : bytes, *args):
return btemplate % tuple(map(to_bytes,args))
render_bytes_template(b'this is how we have to write raw strings with unknown-typed arguments? %s %s %s',5,b'blah','strblah')
# output: b'this is how we have to render raw string templates with unknown-typed arguments? 5 blah strblah'
但在Python 2中,它只是内置的:
'example that just works %s %s %s' % (5,b'blah',u'strblah')
# output: 'example that just works 5 blah strblah'
在Python 3中有没有一种方法可以做到与Python 2相同的性能?请告诉我我是否错过了什么。此处的备选方案是使用cython实现(或者在Python 3中是否有库可以帮助实现?),但仍然不明白为什么除了字符串对象的隐式编码之外,它被从标准库中删除。我们不能只添加一个类似于format_any()的bytes方法吗?
顺便说一句,这并不像一种简单的逃避方式:
def render_bytes_template(btemplate : bytes, *args):
return (btemplate.decode() % args).encode()
我不仅不想进行任何不必要的编码/解码,而且字节参数会被repr而不是原始插入。
'unicode: %s' % (u'Ünîcódæ',)
进行测试。 - Martijn Pieters