擅长:python、mysql、java
<p>我会尝试子类化<a href="http://docs.python.org/2/library/pprint.html#pprint.PrettyPrinter" rel="nofollow">^{<cd1>}</a>。浏览一下源代码,您要覆盖的方法似乎是<code>format(self, object, context, maxlevels, level)</code>:</p>
<pre><code>import pprint
class TruncatingPrettyPrinter(pprint.PrettyPrinter):
def format(self, object, context, maxlevels, level):
if isinstance(object, basestring):
object = object[:72]+"..." # shorten strings to under 80 chars
return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)
TruncatingPrettyPrinter().pprint(quotes)
</code></pre>
<p>这与<code>print str(quotes)</code>并不完全相同,因为<code>pprint</code>包装和排列结构。它还只截断原始对象图中的字符串,而不是任何其他结构的结果字符串表示形式,但它完成了基本工作(没有太宽的输出)。你知道吗</p>