根据文件中模式匹配的键和值形成python字典



我正在尝试创建一个包含基于匹配模式的文本的文件的字典。包含key_str的行应该成为键,随后的行应该成为字典中与这些键相关联的值。

文件:

ml1
/core
/home
ml2
/var
/home
/lib
cpuml1
/home
/root
/raid

预期输出

my_dict: {ml1: ['/core','/home'], ml2: ['/var','/home','/lib'], cpuml1: ['/home','/root','/raid']}
代码:

d = {}
key_str = "ml"
val_str = ""
key_list = []
val_list = []
with open(homedir+'/backup/file2dict.result') as file2dict:
for line in file2dict:
words=line.split()
for aWord in words:
if key_str in aWord:
key_list.append(aWord)
print(key_list)
else:
val_list.append(aWord)
print("this is formed dictionary", d)

每当遇到新键时,将现有的val_list添加到最后一个键并擦除val_list

key_str = "ml"
val_str = ""
val_list = []
key = ''
d = {}
with open(homedir + '/backup/file2dict.result') as file2dict:
for line in file2dict:
words = line.split()
for aWord in words:
if key_str in aWord:
if key:
d[key] = val_list
val_list = []
key = aWord
else:
key = aWord
else:
val_list.append(aWord)
d[key] = val_list
print("this is formed dictionary", d)

假设输入格式是正确的,每个块之间有双换行,每个块以一个键名开始,块中的每一行都是该键的值,并且每个键在文件中是唯一的,以及假设您的意思是['/var','/home', '/lib']ml2键,那么结果可以用一个推导式创建:

with open(file) as f:
result = {key:lst for key, *lst in (block.split('n') for block in f.read().split('nn'))}

用多行字符串代替文件进行测试:

>>> s = '''ml1
... /core
... /home
...
... ml2
... /var
... /home
... /lib
...
... cpuml1
... /home
... /root
... /raid'''
>>> {key:lst for key, *lst in (block.split('n') for block in s.split('nn'))}
{'ml1': ['/core', '/home'], 'ml2': ['/var', '/home', '/lib'], 'cpuml1': ['/home', '/root', '/raid']}

有一种方法:

#! /usr/bin/env python3
keystr="ml"
k=''
d = {}
with open ("testp.txt") as file2dict:
for line in file2dict:
li = line.strip()
# This uses your keystr, but you could say if not line.startswith("/"):
if keystr in li:  
k = li
elif li and k:
d[k] = d.get(k,[]) + [li]

print(d)

你可以简化你的代码:

key_str = "ml"
result = {}
curr_key = None
with open('file2dict.result', 'r') as file2dict:
for line in filter(lambda l: l != '', map(str.strip, file2dict)):
if key_str in line:
curr_key = line
result[curr_key] = []
elif curr_key is not None:
result[curr_key].append(line)
else:
print("Value without a key: {}".format(line))

这里filter(lambda l: l != '', map(str.strip, file2dict))id用来过滤空行;此外,您可以使用dict(result)来收集行。

如果您不能在输入文件中没有任何有效的行(或者如果您想跳过它们),则可以使用setdefault作为您的字典:

key_str = "ml"
result = {}
curr_key = None
with open('file2dict.result', 'r') as file2dict:
for line in filter(lambda l: l != '', map(str.strip, file2dict)):
if key_str in line:
curr_key = line
elif curr_key is not None:
result.setdefault(curr_key, []).append(line)
else:
print("Value without a key: {}".format(line))

最新更新