I have a JSON file from which I want to pick :Description and put in the dictionary Description_List. JSON data :
"UserIntents":[{
"IntentName": "TEAM_WELLNESS",
"ModuleName": "Social",
"Description": "How is the Alchemy team doing?"
},
{
"IntentName": "TEAM_MEMBERS",
"ModuleName": "Social",
"Description": "I'd like to learn more about the team"
}]
Python code :
MN = 'ModuleName'
IN = 'IntentName'
UI = 'UserIntents'
for d in data[UI]:
ModName_Social.setdefault(d[MN], []).append(d[IN])
Description_list.setdefault(d[MN], []).append(d[DL])
Now, the Description_list contains the following output :
Description_list {'Social': ['How is the Alchemy team doing?', "I'd like to learn more about the team"]}
Now, I have created an element from the Element_tree as :
sample_node = SubElement(samples, NODE_CONSTANTS['sample_node'])
which is nothing but a sample node in the trsx file as :
<sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">I'd like to learn more about the team</sample>
<sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">I'd like to learn more about the team</sample>
Now, when I ran the loop inside the Description_list dict, it is being printed that it is trying to set both of the strings to the sample_node element , so the expected output should be like:
<sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">How is the Alchemy team doing</sample>
<sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">I'd like to learn more about the team</sample>
But the current output is :
<sample intentref="SOCIAL_TEAM_WELLNESS" count="1" excluded="true">I'd like to learn more about the team</sample>
<sample intentref="SOCIAL_TEAM_MEMBERS" count="1" excluded="true">I'd like to learn more about the team</sample>
Which is like it is overriding the last value from the Description_list.
Python code :
for key in ModName_Social.keys():
if key not in sample_node:
val = ModName_Social[key]
val1 = Description_list[key]
for i in val:
for j in val1:
if key =='Social':
for i1 in val1:
i_x_0.text = i1 #gets only I'd like to learn more about the team
#i_x_0.text = ''.join(i1) #gets again I'd like to learn more about the team
CodePudding user response:
This might work considering you want to extract one by one from the list and assign to the node
for i1 in val1:
length = len(val1)
i1 =0
while i1 < length:
i_x_0.text = ''.join(val1[i1])
i1 = 1