简体   繁体   English

在Python中使用pprint打印嵌套字典不一致

[英]Inconsistent printing of nested dict using pprint in Python

I used pprint to pretty print a large nested dict : 我用pprint漂亮地打印了一个大的嵌套dict

import pprint
import json


with open('config.json', 'r') as fp:
    conf = fp.read()


pprint.pprint(json.loads(conf))



{u'cust1': {u'videotron': {u'temperature': u'3000K',
                           u'image_file': u'bloup.raw',
                           u'light_intensity': u'20',
                           u'size': [1920, 1080],
                           u'patches': [[94, 19, 247, 77],
                                        [227, 77, 293, 232],
                                        [77, 217, 230, 279],
                                        [30, 66, 93, 211]]}},
 u'cust2': {u'Rogers': {u'accuracy': True,
                        u'bleed': True,
                        u'patches': [[192,
                                      126,
                                      10,
                                      80],
                                     [318,
                                      126,
                                      10,
                                      80], ...

The 2nd level list cust2.Rogers.patches is unfold whereas cust1.videotron.patches is not. 第二级列表cust2.Rogers.patches展开,而cust1.videotron.patches不展开。 I'd like both not to be unfold, ie printed on the same line. 我希望两者都不要展开,即打印在同一行上。 Does anyone know how? 有人知道吗?

You can play with two parameters: width and compact (the last one may be not available for Python 2). 您可以使用两个参数: widthcompact (最后一个参数可能不适用于Python 2)。

width -- limits horizontal space. width -限制水平空间。

And here is description for compact : 这是compact描述:

If compact is false (the default) each item of a long sequence will be formatted on a separate line. 如果compact为false(默认设置),则长序列的每个项目都将在单独的行上格式化。 If compact is true, as many items as will fit within the width will be formatted on each output line. 如果compact为true,则将在每条输出行上格式化所有适合宽度的项目。

But as I understand you can't tell pprint anything about the structure of data and how you want specific elements to be printed. 但是据我了解,您无法告诉pprint任何有关数据结构以及如何打印特定元素的信息。

PrettyPrinter controls PrettyPrinter控件

The PrettyPrinter in the pprint module accepts various parameters to control output formatting: pprint模块中的PrettyPrinter接受各种参数来控制输出格式:

  • indent : the amount of indentation added for each recursive level indent :为每个递归级别添加的缩进
  • width : the desired output width is constrained using the width parameter width :使用width参数限制所需的输出宽度
  • depth : the number of levels which may be printed 深度 :可以打印的级别数
  • compact : when true, as many items as will fit within the width will be formatted on each output line compact :为true时,将在每条输出行上格式化所有适合宽度的项目

JSON alternative JSON替代

The json module itself has its own alternative to pprint using json.dumps with the indent parameter set: json模块本身有自己的替代方法,可以使用带有indent参数集的json.dumps使用pprint

>>> print json.dumps(conf, indent=4)
{
    "cust2": {
        "Rogers": {
            "patches": [
                [
                    192, 
                    126, 
                    10, 
                    80
                ], 
       ...

Specific Problem 具体问题

The 2nd level list cust2.Rogers.patches is unfold whereas cust1.videotron.patches is not. 第二级列表cust2.Rogers.patches展开,而cust1.videotron.patches不展开。 I'd like both not to be unfold, ie printed on the same line. 我希望两者都不要展开,即打印在同一行上。

Neither of the above tools lets you directly solve your problem as specified. 以上两种工具均不能使您直接解决所指定的问题。 To get exactly what you want, you will need to write some custom pretty printing code. 为了得到您想要的,您将需要编写一些自定义漂亮的打印代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM