当将字典附加到列表时,dict。每次都不在一条新线上 [英] When appending dictionary to a list, dict. not on a new line each time

查看:155
本文介绍了当将字典附加到列表时,dict。每次都不在一条新线上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个功能,将由另一个功能创建的字典推送到列表。我使用我的代码的问题是,多个按钮不会在列表中显示在每一行的新行中。我尝试了很多方法,但没有人给我所需的结果。

I have this function that pushes a dictionary created by another function, to a list. The problem I'm having with my code is that multiple pushes do not show up in the list each in a new line. I've tried many ways, but none have given me the desired results.

这是推动创建的字典的行:

This is the line the pushes the created dictionary:

Database.xoomDatabase.append(ordenOrganiz)

此函数创建字典:

def orderZoom(self):
        nombre = contents1
        nicenum = orderResult
        email = contents2
        num = contents3
        fechaentrega = contents5

        global ordenOrganiz
        ordenOrganiz = {"Num Orden": nicenum,
                        "Nombre": nombre, 
                        "Email": email,
                        "Num Tel/Cel": num,
                        "Fecha de Entrega": fechaentrega}
        return ordenOrganiz

任何有关完成此操作的想法?

Any ideas on getting this done?

推荐答案

听起来像你的问题不是插入,而是p请输入以下示例,使用 json.dumps

Sounds like your problem is not the inserts but rather the "pretty print" that you want to apply, check out the following example that uses json.dumps:

import json

ordenOrganiz = {"Num Orden": 1,
                "Nombre": 2, 
                "Email": 3,
                "Num Tel/Cel": 4,
                "Fecha de Entrega": 5}
lst = []
lst.append(ordenOrganiz)                
lst.append(ordenOrganiz)                

print json.dumps(lst, indent=4, separators=(',', ': '))

OUTPUT

[
    {
        "Fecha de Entrega": 5,
        "Nombre": 2,
        "Num Tel/Cel": 4,
        "Num Orden": 1,
        "Email": 3
    },
    {
        "Fecha de Entrega": 5,
        "Nombre": 2,
        "Num Tel/Cel": 4,
        "Num Orden": 1,
        "Email": 3
    }
]

这篇关于当将字典附加到列表时,dict。每次都不在一条新线上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆