Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中使用pprint打印嵌套dict时不一致_Python_Json_Printing_Console_Pprint - Fatal编程技术网

在Python中使用pprint打印嵌套dict时不一致

在Python中使用pprint打印嵌套dict时不一致,python,json,printing,console,pprint,Python,Json,Printing,Console,Pprint,我用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'blou

我用
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], ...

第二级列表
cust2.Rogers.patches
展开,而
cust1.videotron.patches
未展开。我希望两个都不要展开,即打印在同一行上。有人知道怎么做吗?

您可以使用两个参数:
width
compact
(最后一个参数可能不适用于Python 2)

宽度
——限制水平空间

下面是对
compact
的说明:

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], ...
如果compact为false(默认值),则长序列的每个项都将在单独的行上格式化。如果compact为true,则在每个输出行上格式化宽度范围内的尽可能多的项

但据我所知,您无法告诉
pprint
任何有关数据结构以及您希望如何打印特定元素的信息。

PrettyPrinter控件 pprint模块中的接受各种参数以控制输出格式:

  • 缩进量:为每个递归级别添加的缩进量
  • 宽度:使用宽度参数约束所需的输出宽度
  • 深度:可以打印的级别数
  • 压缩:如果为true,则在每个输出行上格式化宽度范围内的尽可能多的项目
JSON替代方案 与pprint一起使用
indent
参数集时,本身有自己的替代方案:

>>> print json.dumps(conf, indent=4)
{
    "cust2": {
        "Rogers": {
            "patches": [
                [
                    192, 
                    126, 
                    10, 
                    80
                ], 
       ...
具体问题 第二级列表cust2.Rogers.patches已展开,而cust1.videotron.patches未展开。我不希望两者都展开,即打印在同一行上


上述两种工具都不允许您按照指定的方式直接解决问题。为了得到您想要的东西,您需要编写一些定制的漂亮打印代码。

谢谢danil,我确实看到了compact,但我使用的是py2。我不认为我的问题来自有限的
宽度
,就是这样,谢谢!