Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.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的str输出_Python_Python 2.7 - Fatal编程技术网

如何格式化python的str输出

如何格式化python的str输出,python,python-2.7,Python,Python 2.7,我正在处理OpenStack,我看到很多对象被写入日志文件中的一长行字符串中。例如 vol={'migration_status': None, 'availability_zone': u'nova', 'terminated_at': None, 'updated_at': datetime.datetime(2015, 5, 15, 7, 0, 11), 'provider_geometry': None, 'snapshot_id': None, 'ec2_id': None, 'mou

我正在处理OpenStack,我看到很多对象被写入日志文件中的一长行字符串中。例如

vol={'migration_status': None, 'availability_zone': u'nova', 'terminated_at': None, 'updated_at': datetime.datetime(2015, 5, 15, 7, 0, 11), 'provider_geometry': None, 'snapshot_id': None, 'ec2_id': None, 'mountpoint': u'/dev/sdb', 'deleted_at': None, 'id': u'83a5ade7-275e-4ffd-b494-a4c2c9a169d1', 'size': 10L, 'user_id': u'2b5cd66674d24c4f94138002d96f5f96', 'attach_time': u'2015-05-13T06:12:35.749100', 'attached_host': None, 'display_description': None, 'volume_admin_metadata': [<cinder.db.sqlalchemy.models.VolumeAdminMetadata object at 0x7fbd834f52d0>, <cinder.db.sqlalchemy.models.VolumeAdminMetadata object at 0x7fbd834f58d0>], 'encryption_key_id': None, 'project_id': u'9ba90732bdba43dcbd49e111cbe92458', 'launched_at': datetime.datetime(2015, 5, 13, 5, 56, 33), 'scheduled_at': datetime.datetime(2015, 5, 13, 5, 56, 31), 'status': u'available', 'volume_type_id': None, 'deleted': False, 'provider_location': None, 'host': u'controller01', 'source_volid': None, 'provider_auth': None, 'display_name': u'parallel_200_1-my_vol107-umrnk5jchjwo', 'instance_uuid': u'a81b355d-d240-4888-93a1-7e10822a777f', 'bootable': False, 'created_at': datetime.datetime(2015, 5, 13, 5, 56, 31), 'attach_status': u'attached', 'volume_type': None, '_name_id': None, 'volume_metadata': [], 'metadata': {u'readonly': u'False', u'attached_mode': u'rw'}}
我怎么能这么做

我有一个后续问题。所有答案都涉及到建立一个dict或list对象。但如果日志文件中的字符串中包含以下内容,则会出现错误:

  • 'volume\u admin\u metadata':[,]-错误:语法错误:无效语法(在“Python中有一个可以像您描述的那样格式化输出的语法)

    >>> import pprint
    >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
    >>> stuff.insert(0, stuff[:])
    >>> pp = pprint.PrettyPrinter(indent=4)
    >>> pp.pprint(stuff)
    [   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
        'spam',
        'eggs',
        'lumberjack',
        'knights',
        'ni']
    
    对于在
    \uuu str\uuu
    方法中使用,
    pprint.pformat
    将是最方便的函数。

    Python有一个可以像您描述的那样格式化输出的函数

    >>> import pprint
    >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
    >>> stuff.insert(0, stuff[:])
    >>> pp = pprint.PrettyPrinter(indent=4)
    >>> pp.pprint(stuff)
    [   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
        'spam',
        'eggs',
        'lumberjack',
        'knights',
        'ni']
    
    对于在
    \uuu str\uuu
    方法中使用,
    pprint.pformat
    将是最方便的函数。

    Python有一个可以像您描述的那样格式化输出的函数

    >>> import pprint
    >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
    >>> stuff.insert(0, stuff[:])
    >>> pp = pprint.PrettyPrinter(indent=4)
    >>> pp.pprint(stuff)
    [   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
        'spam',
        'eggs',
        'lumberjack',
        'knights',
        'ni']
    
    对于在
    \uuu str\uuu
    方法中使用,
    pprint.pformat
    将是最方便的函数。

    Python有一个可以像您描述的那样格式化输出的函数

    >>> import pprint
    >>> stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni']
    >>> stuff.insert(0, stuff[:])
    >>> pp = pprint.PrettyPrinter(indent=4)
    >>> pp.pprint(stuff)
    [   ['spam', 'eggs', 'lumberjack', 'knights', 'ni'],
        'spam',
        'eggs',
        'lumberjack',
        'knights',
        'ni']
    

    对于在
    \uuu str\uuu
    方法中使用,
    pprint.pformat
    将是最方便的功能。

    除了
    pprint
    之外,您还可以尝试
    json

    >>> import json
    >>> jhon = {'name': 'Jhon', 'age': 12, 'hobbies': ['football', 'games', 'running'], 'family': {'father': 'David', 'mother': 'Rose'}}
    >>> print(json.dumps(jhon, indent=4))
    {
        "age": 12, 
        "name": "jhon", 
        "family": {
            "father": "David", 
            "mother": "Rose"
        }, 
        "hobbies": [
            "football", 
            "games", 
            "running"
        ]
    }
    

    由于您有类似于“updated_at”的东西:datetime.datetime(2015,5,15,7,0,11),您可以使用
    json.dumps(blablabla,indent=4,default=str)

    除了
    pprint
    之外,您还可以尝试
    json

    >>> import json
    >>> jhon = {'name': 'Jhon', 'age': 12, 'hobbies': ['football', 'games', 'running'], 'family': {'father': 'David', 'mother': 'Rose'}}
    >>> print(json.dumps(jhon, indent=4))
    {
        "age": 12, 
        "name": "jhon", 
        "family": {
            "father": "David", 
            "mother": "Rose"
        }, 
        "hobbies": [
            "football", 
            "games", 
            "running"
        ]
    }
    

    由于您有类似于“updated_at”的东西:datetime.datetime(2015,5,15,7,0,11),您可以使用
    json.dumps(blablabla,indent=4,default=str)

    除了
    pprint
    之外,您还可以尝试
    json

    >>> import json
    >>> jhon = {'name': 'Jhon', 'age': 12, 'hobbies': ['football', 'games', 'running'], 'family': {'father': 'David', 'mother': 'Rose'}}
    >>> print(json.dumps(jhon, indent=4))
    {
        "age": 12, 
        "name": "jhon", 
        "family": {
            "father": "David", 
            "mother": "Rose"
        }, 
        "hobbies": [
            "football", 
            "games", 
            "running"
        ]
    }
    

    由于您有类似于“updated_at”的东西:datetime.datetime(2015,5,15,7,0,11),您可以使用
    json.dumps(blablabla,indent=4,default=str)

    除了
    pprint
    之外,您还可以尝试
    json

    >>> import json
    >>> jhon = {'name': 'Jhon', 'age': 12, 'hobbies': ['football', 'games', 'running'], 'family': {'father': 'David', 'mother': 'Rose'}}
    >>> print(json.dumps(jhon, indent=4))
    {
        "age": 12, 
        "name": "jhon", 
        "family": {
            "father": "David", 
            "mother": "Rose"
        }, 
        "hobbies": [
            "football", 
            "games", 
            "running"
        ]
    }
    
    由于您有类似于“updated_at”的东西:datetime.datetime(2015,5,15,7,0,11),您可以使用
    json.dumps(blablabla,indent=4,default=str)