擅长:python、mysql、java
<p>基于Dirk Stócker的答案,这里有一个用于Python3打印函数的简洁包装函数。使用它就像使用打印一样。</p>
<p>作为额外的好处,与其他答案相比,这不会将文本打印为bytearray('b“content“),而是作为普通字符串('content'),因为最后一个解码步骤。</p>
<pre><code>def uprint(*objects, sep=' ', end='\n', file=sys.stdout):
enc = file.encoding
if enc == 'UTF-8':
print(*objects, sep=sep, end=end, file=file)
else:
f = lambda obj: str(obj).encode(enc, errors='backslashreplace').decode(enc)
print(*map(f, objects), sep=sep, end=end, file=file)
uprint('foo')
uprint(u'Antonín Dvořák')
uprint('foo', 'bar', u'Antonín Dvořák')
</code></pre>