如何在字典里读元组

2024-10-03 06:27:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我的问题是,我收到了这个,但在字符串中

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']来改变值12.00到1200,当然使用int,我知道怎么做,只需要乘以100 例如

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

但是我不知道如何用linea['itbis]..pagos['importe']和descoutos['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

但我犯了个错误 回溯(最近一次呼叫): 文件“”,第2行,在 TypeError:“bool”对象不可iterable

好吧,谢谢你读了这么长的解释


Tags: falseobjhereparaverpagoimportelinea
1条回答
网友
1楼 · 发布于 2024-10-03 06:27:54

之所以会出现这个错误,是因为dict中并非所有的值都是iterable。如果您想迭代整个dict以找到您描述的值(即,您不确切地知道dict的内容),您可以这样做:

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

if hasattr(v, '__iter__'):行将告诉您的项是否可iterable(请参阅这个问题In Python, how do I determine if an object is iterable?

相关问题 更多 >