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

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

问题描述

我有这个函数将另一个函数创建的字典推送到一个列表.我的代码遇到的问题是多个推送没有显示在列表中的每一个新行中.我尝试了很多方法,但都没有给我想要的结果.

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

Database.xoomDatabase.append(ordenOrganiz)

这个函数创建字典:

def orderZoom(self):名称 = 内容 1nicenum = orderResult电子邮件 = 内容 2数量 = 内容 3fechaentrega = 内容5全球秩序组织ordenOrganiz = {"Num Orden": nicenum,名词":名词,电子邮件":电子邮件,电话号码/电话号码":号码,"Fecha de Entrega": fechaentrega}退货令OrdenOrganiz

关于完成这项工作有什么想法吗?

解决方案

听起来您的问题不是插入,而是您想要应用的漂亮打印",请查看以下使用 json 的示例.转储:

导入jsonordenOrganiz = {"Num Orden": 1,"Nombre": 2,电子邮件":3,电话号码/电话号码":4,Fecha de Entrega":5}lst = []lst.append(ordenOrganiz)lst.append(ordenOrganiz)打印 json.dumps(lst, indent=4, separators=(',', ': '))

输出

<预><代码>[{Fecha de Entrega":5,"Nombre": 2,电话号码/电话号码":4,"Num Orden": 1,电子邮件":3},{Fecha de Entrega":5,"Nombre": 2,电话号码/电话号码":4,"Num Orden": 1,电子邮件":3}]

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)

This function creates the dictionary:

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?

解决方案

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天全站免登陆