python - 检查大于一个嵌套字典的键
问题描述
我有一个看起来像的文件:
{"phrase": "can you please talk with Mark and say school/workout/home/homework with slack", "entities": [{ "label": "name", "match": "Mark"}, { "label": "message"}]}
{"phrase": "can you please tweet must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6 on whatsapp", "template": "can you please tweet <message> on whatsapp", "entities": [{ "label": "message", "match": "must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6"}]}
我需要检查文件的每一行,看看“实体”键是否有多个嵌套字典。如果是这样,那么我需要访问该字典并添加一个匹配列,以便文件看起来像:
{"phrase": "can you please talk with Mark and say school/workout/home/homework with slack", "entities": [{ "label": "name", "match": "Mark"}, { "label": "message", "match": "school"}]}
{"phrase": "can you please tweet must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6 on whatsapp", "template": "can you please tweet <message> on whatsapp", "entities": [{ "label": "message", "match": "must_not_put_on_heating_electric_costs_raised_by_\ufffd30pm_as_of_1/6"}]}
我真的不知道该怎么做?
到目前为止我的代码:
for row in data:
entities_len = len(row['entities'])
if entities_len >2 :
try:
row['target_entities'][1]['match'] = 'test'
print(row)
这没有按预期填充第二场比赛。
解决方案
推荐阅读
- c# - 为什么我不能在 Visual Studio 2012 和 2015 中添加对项目的引用?
- javascript - 减少函数中的变异累加器是否被认为是不好的做法?
- c# - 绑定到代码隐藏动态变量在多重绑定中不起作用
- pandas - 如何删除两列中的相同单词?两行重复
- reactjs - ReactJS:如何渲染对象集合
- git - github/git Checkout 在 Windows 上返回“错误:无效路径”
- c - 我一直在“hiddenLetters[i] = '_';”行中看到取消引用空指针警告?
- c# - 订阅热巧克力“RESULT_TYPE_NOT_SUPPORTED”
- apache-spark - 使用 dataframe.schema VS dataframe.printSchema() 比较 pyspark 模式
- android - WifiNetworkSpecifier 不会触发编译时错误