通过包含字符串列表的字典在 trsx 元素节点中添加文本
Add text in the trsx Element Node via a dictionary containing list of Strings
我有一个 JSON 文件,我想从中选择 :Description 并放入字典 Description_List。
JSON 数据:
"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代码:
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])
现在,Description_list 包含以下输出:
Description_list {'Social': ['How is the Alchemy team doing?', "I'd like to learn more about the team"]}
现在,我从 Element_tree 创建了一个元素,如下所示:
sample_node = SubElement(samples, NODE_CONSTANTS['sample_node'])
这只是 trsx 文件中的示例节点:
<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>
现在,当我 运行 Description_list 字典中的循环时,正在打印它试图将两个字符串都设置为 sample_node 元素,所以预期输出应该是这样的:
<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>
但是当前输出是:
<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>
这就像覆盖了 Description_list 中的最后一个值。
Python代码:
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
考虑到您想从列表中逐一提取并分配给节点,这可能会奏效
for i1 in val1:
length = len(val1)
i1 =0
while i1 < length:
i_x_0.text = ''.join(val1[i1])
i1 += 1
我正在对此进行更多检查,我相信以下内容将解决您最重要的问题:
if type(Description_list[key]) is list:
if a1 =='SOCIAL_TEAM_WELLNESS':
i_x_0.text =Description_list[key][0]
else:
i_x_0.text = j
我有一个 JSON 文件,我想从中选择 :Description 并放入字典 Description_List。 JSON 数据:
"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代码:
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])
现在,Description_list 包含以下输出:
Description_list {'Social': ['How is the Alchemy team doing?', "I'd like to learn more about the team"]}
现在,我从 Element_tree 创建了一个元素,如下所示:
sample_node = SubElement(samples, NODE_CONSTANTS['sample_node'])
这只是 trsx 文件中的示例节点:
<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>
现在,当我 运行 Description_list 字典中的循环时,正在打印它试图将两个字符串都设置为 sample_node 元素,所以预期输出应该是这样的:
<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>
但是当前输出是:
<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>
这就像覆盖了 Description_list 中的最后一个值。
Python代码:
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
考虑到您想从列表中逐一提取并分配给节点,这可能会奏效
for i1 in val1:
length = len(val1)
i1 =0
while i1 < length:
i_x_0.text = ''.join(val1[i1])
i1 += 1
我正在对此进行更多检查,我相信以下内容将解决您最重要的问题:
if type(Description_list[key]) is list:
if a1 =='SOCIAL_TEAM_WELLNESS':
i_x_0.text =Description_list[key][0]
else:
i_x_0.text = j