擅长:python、mysql、java
<p>给出:</p>
<pre><code>>>> my_array = numpy.array(['apple', 'pear'], dtype = 'S5')
</code></pre>
<p>你可以在飞行中解码:</p>
<pre><code>>>> print("Mary has an {} and a {}".format(*map(lambda b: b.decode('utf-8'), my_array)))
Mary has an apple and a pear
</code></pre>
<p>或者可以创建特定的格式化程序:</p>
<pre><code>import string
class ByteFormatter(string.Formatter):
def __init__(self, decoder='utf-8'):
self.decoder=decoder
def format_field(self, value, spec):
if isinstance(value, bytes):
return value.decode(self.decoder)
return super(ByteFormatter, self).format_field(value, spec)
>>> print(ByteFormatter().format("Mary has an {} and a {}", *my_array))
Mary has an apple and a pear
</code></pre>