Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.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 如何阅读字典中的元组_Python_Dictionary_Int_Tuples - Fatal编程技术网

Python 如何阅读字典中的元组

Python 如何阅读字典中的元组,python,dictionary,int,tuples,Python,Dictionary,Int,Tuples,我的问题是,我收到了这封信,但它是一串的 obj = {"rnc": "44444044444", "cliente": "EMPRESA S.A.", "ncf": "1234567890123456789", "ncf_ref": "0987654321098765432", "tipo": "FacturaConsumidorFinal", # Ver clase ReceiptEnum para valore

我的问题是,我收到了这封信,但它是一串的

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['import']来改变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']和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
    }
但我犯了个错误,结果 回溯(最近一次呼叫最后一次): 文件“”,第2行,在 TypeError:“bool”对象不可编辑


感谢您阅读这篇冗长的解释,您会得到这个错误,因为您的dict中并非所有的值都是不可编辑的。如果您想迭代整个dict以找到您描述的值(即,您不确切知道dict的内容),可以执行以下操作:

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

如果hasattr(v,'.'.'iter'.'':行将告诉您的项是否可编辑(请参阅此问题)

好吧,您的一些值是
False
,并且,正如错误消息所说,您不能迭代布尔值。看起来您的dict值是一个包含单个元素的列表,这是一个dict。您应该能够使用
linea[“lineas”][0][“Import”]
来访问
12.00
条目。然后您可以对其执行任何操作。我尝试使用linea[“lineas”][0][“Import”]但我得到了以下类型错误:元组索引必须是整数,而不是strthank您…但我如何访问那些我猜它是调用列表来执行操作的。。?
for k,v in obj.items():
    if hasattr(v, '__iter__'):
        for thing in v:
            print thing
    else:
        print v