Stack Overflow Asked on November 10, 2021
I have created dictionary as mentioned below. from this dictionary , i am writing keys an values into a file.
comp_dict = { 1:
{ 'partname' : 'one' ,
'jar': [{'soa_jar_name' : 'jar1.jar' , 'archivename' : 'archjar1.jar'},
{'soa_jar_name' : 'jar2.jar' , 'archivename' : 'archjar2.jar'},
{'soa_jar_name' : 'jar3.jar' , 'archivename' : 'archjar3.jar'}]},
2:
{ 'partname' : 'two' , 'jar':
[{'soa_jar_name' : 'jarany.jar' , 'archivename' : 'archjarany.jar'},
{'soa_jar_name' : 'jarmine.jar' , 'archivename' : 'archjarmine.jar'},
{'soa_jar_name' : 'jarnew1.jar' , 'archivename' : 'archjarnew1.jar'}]}}
I have used below code to get the results.
for key in comp_dict:
for listitem in comp_dict[key]['jar']:
inner_dict = dict(listitem)
print('partname: {}'.format(comp_dict[key]['partname']))
for inner_key, inner_value in inner_dict.items():
print('t{}: {}'.format(inner_key, inner_value))
print()
This is above code results.
partname: one
soa_jar_name: jar1.jar
archivename: archjar1.jar
partname: one
soa_jar_name: jar2.jar
archivename: archjar2.jar
partname: one
soa_jar_name: jar3.jar
archivename: archjar3.jar
partname: two
soa_jar_name: jarany.jar
archivename: archjarany.jar
partname: two
soa_jar_name: jarmine.jar
archivename: archjarmine.jar
But i need to add number in each keys section mentioned below.
partname.1: one
soa_jar_name.1: jar1.jar
archivename.1: archjar1.jar
partname.2: one
soa_jar_name.2: jar2.jar
archivename.2: archjar2.jar
partname.3: one
soa_jar_name.3: jar3.jar
archivename.3: archjar3.jar
partname.4: two
soa_jar_name.4: jarany.jar
archivename.4: archjarany.jar
partname.5: two
soa_jar_name.5: jarmine.jar
archivename.5: archjarmine.jar
partname.6: two
soa_jar_name.6: jarnew1.jar
archivename.6: archjarnew1.jar
Can anyone please help me here
Try:
count = 1
for key in comp_dict:
for listitem in comp_dict[key]['jar']:
inner_dict = dict(listitem)
print(f"partname.{count}: {comp_dict[key]['partname']}")
for inner_key, inner_value in inner_dict.items():
print(f"t{inner_key}.{count}: {inner_value}")
print()
count+=1
partname.1: one
soa_jar_name.1: jar1.jar
archivename.1: archjar1.jar
partname.2: one
soa_jar_name.2: jar2.jar
archivename.2: archjar2.jar
partname.3: one
soa_jar_name.3: jar3.jar
archivename.3: archjar3.jar
partname.4: two
soa_jar_name.4: jarany.jar
archivename.4: archjarany.jar
partname.5: two
soa_jar_name.5: jarmine.jar
archivename.5: archjarmine.jar
partname.6: two
soa_jar_name.6: jarnew1.jar
archivename.6: archjarnew1.jar
Answered by Pygirl on November 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP