0

My question is i recive this but in string

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
        }

I convert to dictionary using

import ast
linea = ast.literal_eval(obj)

im trying to read inside lineas['importe'] to change value 12.00 to 1200 of course using int, i know how to do it, just gonna multiply for 100 for example

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

but i dont know how to get there the same with linea['itbis]..pagos['importe'] and descuentos['importe'] and the end it will output like this.

{"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
    }

I was trying to

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

but i got an error and the end Traceback (most recent call last): File "", line 2, in TypeError: 'bool' object is not iterable

well Thanks for read this long explanation x_X

  • 1
    Well, some of your values are `False`, and, as the error message says, you can't iterate over a boolean. – Morgan Thrapp Feb 11 '16 at 15:26
  • It looks like your dict value is a list with a single element, which is a dict. You should be able to use `linea["lineas"][0]["importe"]` to access the `12.00` entry. You can then do whatever you like to it. – Tom Karzes Feb 11 '16 at 15:27
  • I try using linea["lineas"][0]["importe"] but i got this TypeError: tuple indices must be integers, not str – Jorge Tejeda Feb 11 '16 at 15:37

1 Answers1

0

You get this error because not all values in your dict are iterables. If you want to iterate over the whole dict to find the values you describe (i.e. you don't know exactly the contents of the dict), you can do something like this:

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

The line if hasattr(v, '__iter__'): will tell if your item is iterable (refer to this question In Python, how do I determine if an object is iterable?)

Community
  • 1
  • 1
Cantfindname
  • 2,008
  • 1
  • 17
  • 30