我的问题是,我收到了这个,但在字符串中
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
好吧,谢谢你读了这么长的解释
之所以会出现这个错误,是因为dict中并非所有的值都是iterable。如果您想迭代整个dict以找到您描述的值(即,您不确切地知道dict的内容),您可以这样做:
第
if hasattr(v, '__iter__'):
行将告诉您的项是否可iterable(请参阅这个问题In Python, how do I determine if an object is iterable?)相关问题 更多 >
编程相关推荐