繁体   English   中英

如何在字典中阅读元组

[英]How to read a tuple inside dictionary

我的问题是我接受了这个但字符串

obj = {"rnc": "44444044444",
         "cliente": "EMPRESA S.A.",
         "ncf": "1234567890123456789",
         "ncf_ref": "0987654321098765432",
         "tipo": "FacturaConsumidorFinal",     # Ver clase ReceiptEnum para valores permitidos
         "logo": false,
         "lineas": [{
             "descripcion": ["Linea 1", ..., "Linea 10"],
             "cantidad": 2,
             "importe": 12.00,
             "itbis": 13.0,
             "tipo_pago": "LineaVenta",        # Ver clase ReceiptItemEnum para valores permitidos
             "qtyxprice": True,
             "promocion": False"
            }],
         "pagos": [{
            "tipo": 1,                         # valor entre 1 y 14, según tipos de pago configurados
            "importe": 1200.00,
            "cancelado": False,
            "descripcion": ["linea 1", "linea 2", "lines 3"],
         }],
         "descuentos": [{                      # descuento o recargo global
            "descripcion": "lalalala",
            "importe": 1200.00
         }],
         "densidad": "ppp180x180"              # ver clase PrinterDensity para valores permitidos
        }

我使用转换为字典

import ast
linea = ast.literal_eval(obj)

我试图读取lineas ['importe']里面的值,当然使用int将12.00更改为1200,我知道该怎么做,例如要乘以100

#12.00
int(12.00*100) = 1200 #they always need to end with '00'
int(2*100) = 200

但是我不知道如何使用linea ['itbis] .. pagos ['importe']和descuentos ['importe']到达那里,最终它将像这样输出。

{"rnc": "44444044444",
     "cliente": "EMPRESA S.A.",
     "ncf": "1234567890123456789",
     "ncf_ref": "0987654321098765432",
     "tipo": "FacturaConsumidorFinal",     # Ver clase ReceiptEnum para valores permitidos
     "logo": false,
     "lineas": [{
         "descripcion": ["Linea 1", ..., "Linea 10"],
         "cantidad": 2,
         "importe": 1200,    #<----- here(12.00)
         "itbis": 1300,      #<----- here(13.00)
         "tipo_pago": "LineaVenta",        # Ver clase ReceiptItemEnum para valores permitidos
         "qtyxprice": True,
         "promocion": False"
        }],
     "pagos": [{
        "tipo": 1,                         # valor entre 1 y 14, según tipos de pago configurados
        "importe": 120000,     #<----- here (1200.00)
        "cancelado": False,
        "descripcion": ["linea 1", "linea 2", "lines 3"],
     }],
     "descuentos": [{                      # descuento o recargo global
        "descripcion": "lalalala",
        "importe": 120000   #<----- here (1200.00)
     }],
     "densidad": "ppp180x180"              # ver clase PrinterDensity para valores permitidos
    }

我试图

for k,v in obj.items():
for thing in v:
    print v

但是我遇到了一个错误,并且结束了Traceback(最近一次调用):TypeError中的文件“”,第2行:“ bool”对象不可迭代

好,谢谢您阅读这个详尽的解释x_X

您会收到此错误,因为并非dict中的所有值都是可迭代的。 如果要遍历整个字典以找到您描述的值(即您不完全知道字典的内容),则可以执行以下操作:

for k,v in obj.items():
    if hasattr(v, '__iter__'):
        for thing in v:
            print thing
    else:
        print v

if hasattr(v, '__iter__'):行将告诉您的项目是否可迭代(请参阅此问题, 在Python中,如何确定对象是否可迭代?

暂无
暂无

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

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