如何保存json在同一列或在一个新的变化?



我想知道我可以向代码中添加什么,以便它将json保存在同一列中或保存在新列中,但要使用我通过条件所做的更改(例如使用包含的更改覆盖它)。我不是那么熟练的python,但我正在学习,如果你能帮助我,我真的很感激。

for i in xs2['input_parameters2']:
datos = json.loads(i)
#new = []
#print(datos['minuta_id'])
for companias in datos['companies']:
if 'financial_accounts' in 'companies':
for dict1 in range(len(xs2)):
if companias['ruc'] == xs2.loc[dict,'RUC']:
#companias['financial_accounts'].update(xs2.loc[dict,'dic'])
companias['financial_accounts'] =[
{"code":371,"name":"Inversiones en valores","value":xs2.loc[dict,'INVERSIONES_EN_VALORES']},
{"code":408,"name":"Capital","value":xs2.loc[dict,'CAPITAL_FIJO']},
{"code":414,"name":"Resultados acumulados","value":xs2.loc[dict,'RESULTADOS_ACUMULADOS']},
{"code":415,"name":"Resultado del ejercicio","value":xs2.loc[dict,'RESULTADOS_DEL_EJERCICIO']},
{"code":416,"name":"Ventas","value":xs2.loc[dict,'VENTAS_NETAS']}]
print(companias['financial_accounts'])
print(companias['ruc'])      

else:
for dict in range(len(xs2)):
if companias['ruc'] == xs2.loc[dict,'RUC']:
companias['financial_accounts'] =[
{"code":371,"name":"Inversiones en valores","value":xs2.loc[dict,'INVERSIONES_EN_VALORES']},
{"code":408,"name":"Capital","value":xs2.loc[dict,'CAPITAL_FIJO']},
{"code":414,"name":"Resultados acumulados","value":xs2.loc[dict,'RESULTADOS_ACUMULADOS']},
{"code":415,"name":"Resultado del ejercicio","value":xs2.loc[dict,'RESULTADOS_DEL_EJERCICIO']},
{"code":416,"name":"Ventas","value":xs2.loc[dict,'VENTAS_NETAS']}]
#print(new)

您不需要if 'financial_accounts' in 'companies':语句,因为您在ifelse中都做了相同的事情。无条件地去做。

for i in xs2['input_parameters2']:
datos = json.loads(i)
#new = []
#print(datos['minuta_id'])
for companias in datos['companies']:
for dict1 in range(len(xs2)):
if companias['ruc'] == xs2.loc[dict,'RUC']:
#companias['financial_accounts'].update(xs2.loc[dict,'dic'])
companias['financial_accounts'] =[
{"code":371,"name":"Inversiones en valores","value":xs2.loc[dict,'INVERSIONES_EN_VALORES']},
{"code":408,"name":"Capital","value":xs2.loc[dict,'CAPITAL_FIJO']},
{"code":414,"name":"Resultados acumulados","value":xs2.loc[dict,'RESULTADOS_ACUMULADOS']},
{"code":415,"name":"Resultado del ejercicio","value":xs2.loc[dict,'RESULTADOS_DEL_EJERCICIO']},
{"code":416,"name":"Ventas","value":xs2.loc[dict,'VENTAS_NETAS']}]
print(companias['financial_accounts'])
print(companias['ruc'])      
break

不需要测试financial_accounts是否已经在字典中。如果它已经存在,赋值将替换它,否则将创建它。

只有当您想要将新值与旧值组合在一起时才需要这种测试。但是因为你要替换它,旧的值就不重要了。

相关内容

  • 没有找到相关文章

最新更新