【发布时间】:2020-06-10 22:27:07
【问题描述】:
我想在某个关键字之后扫描文本并提取数据。在这种情况下,我正在扫描sql 文件。我的字典的结构是这样的(嵌套的):
my_dict = {sql_file:{'select': table_name, 'join': table_name}}
我有一个循环遍历 sql 文件,找到关键字并为目录中的每个文件提取表。但是,因为我需要在扫描 select 关键字后更新字典然后返回并更新字典以更新 join 关键字,更新结束覆盖 我的原始字典值。这是循环的工作原理。
for key, val in file_dict.items(): # I have a dictionary that has {filename:sql_script} for key,val
for match in select.finditer(val): # regex iterator to find all matches
if match.group(1):
my_dict[sql_file] = {'select': match.group(1)})
for key, val in file_dict.items(): # I have a dictionary that has {filename:sql_script} for key,val
for match in join.finditer(val): # regex iterator to find all matches
if match.group(1):
my_dict[sql_file] = {'join': match.group(1)})
这最终不会添加select 匹配,然后添加join 匹配,它只是覆盖select 并将其替换为join。字典没有附加方法,所以我不知道如何继续。
【问题讨论】:
-
my_dict[sql_file]['select'] = match.group(1) -
你不需要 append 方法,只需分配你想要的键。
-
你能提供一个minimal reproducible example吗?
标签: python dictionary