Dictionary关键字动态值出错



我的python文件中有一段代码,如下所示。

data_to_parse = """
Repo: 2msdsdodule-losdsdader
Path: 2modsdsule-loasdsdder
Components: 2sssss-sdsds sds-sdsd-test sdsd-load-sdsds

Repo: 1msdsdodule-losdsdader
Path: 1modsdsule-loasdsdder
Components: 1libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds

Repo: 3msdsdodule-losdsdader
Path: 3modsdsule-loasdsdder
Components: 3libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds

"""
lines = data_to_parse.splitlines()#print(lines,'-----')
result_list = []
result = {}

for line in lines:
results = {}
if 'Components:' in line:
line2 = line.split(':')
result['Components']= (line2[1].strip())
if 'Path:' in line:
line2 = line.split(':')
result['Path'] = (line2[1].strip())
results[result['Components']] = result['Path']
result_list.append(results)
print(result_list)

但当我试图运行这个时,它向我显示了错误,比如:

results[result['Components']] = result['Path']
KeyError: 'Components'

.有人能帮我把这个说出来吗??我被困在这里了。任何建议,任何帮助都是重要的。

输出我想要像:

[{'2sssss-sdsds sds-sdsd-test sdsd-load-sdsds':'2msdsdodule-losdsdader'}
{'1libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds':'1msdsdodule-losdsdader'},
{'3libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds':'3msdsdodule-losdsdader
'}]

就像前面提到的kareem一样,循环逐行读取。但是"Path"行位于读取"Component"行之前。要解决此问题,您可以将代码更改为:

lines = data_to_parse.splitlines()#print(lines,'-----')
result_list = []
result = {}

for line in lines:
results = {}
if 'Path:' in line:
line2 = line.split(':')
result['Path'] = (line2[1].strip())

if 'Components:' in line:
line2 = line.split(':')
result['Components']= (line2[1].strip())
results[result['Components']] = result['Path']
result_list.append(results)

不同的是,我们更改了Path和Component if代码行。有了这些代码,您可能会得到您正在寻找的结果。就我个人而言,我建议您使用列表理解来避免定义临时变量。考虑一下做同样事情的代码:

lines = data_to_parse.splitlines()
components = [line.split(':')[1].strip() for line in lines if 'Components' in line]
path = [line.split(':')[1].strip() for line in lines if 'Path' in line]
result_list = [{components[i]:path[i]} for i in range(len(components))]

此代码假定有相等数量的元件线和路径。如果您有特殊条件,您可能需要通过添加条件来更改代码。

出现错误是因为if的第一个执行是第二个执行,因为在第二次迭代中,第二行具有"0";路径";并且dictresult为空;"组件";不存在,则引发错误。

我的解决方案是删除所有空行,并将列表分成3块。

data_to_parse = """
Repo: 2msdsdodule-losdsdader
Path: 2modsdsule-loasdsdder
Components: 2sssss-sdsds sds-sdsd-test sdsd-load-sdsds
Repo: 1msdsdodule-losdsdader
Path: 1modsdsule-loasdsdder
Components: 1libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds
Repo: 3msdsdodule-losdsdader
Path: 3modsdsule-loasdsdder
Components: 3libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds
"""

def chunks(lst, n):
"""Yield successive n-sized chunks from lst."""
for i in range(0, len(lst), n):
yield lst[i:i + n]

lines = [line.strip() for line in data_to_parse.splitlines() if line]  # list without empty lines
result_list = []
for chunk in [chunk for chunk in chunks(lines, 3) if len(chunk) == 3]:
result = {chunk[2]: chunk[1]}
print(result)
result_list.append(result)
data_to_parse = """Repo: 2msdsdodule-losdsdader
Path: 2modsdsule-loasdsdder
Components: 2sssss-sdsds sds-sdsd-test sdsd-load-sdsds
Repo: 1msdsdodule-losdsdader
Path: 1modsdsule-loasdsdder
Components: 1libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds

Repo: 3msdsdodule-losdsdader
Path: 3modsdsule-loasdsdder
Components: 3libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds
"""
# print(data_to_parse)
lines = data_to_parse.splitlines()#print(lines,'-----')
result_list = lines
# print(result_list)
result = {}
# print(result)
for line in result_list:
# results = {}
print(line)
if 'Components:' in line:
line2 = line.split(':')
# print(line2)
result['Components']= (line2[1].strip())
# print(result)
if 'Path:' in line:
line2 = line.split(':')
result['Path'] = (line2[1].strip())
result['Components'] = result['Path']
result_list.append(result)
print(result_list)

另一种方法是这样>gt;

data_to_parse = """
Repo: 2msdsdodule-losdsdader
Path: 2modsdsule-loasdsdder
Components: 2sssss-sdsds sds-sdsd-test sdsd-load-sdsds

Repo: 1msdsdodule-losdsdader
Path: 1modsdsule-loasdsdder
Components: 1libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds

Repo: 3msdsdodule-losdsdader
Path: 3modsdsule-loasdsdder
Components: 3libmsdsodule-sdsds sds-sdsd-test sdsd-load-sdsds

"""
lines = data_to_parse.splitlines()
my_val = ''
result_list = []
for line in lines:
clean_line = line.strip()  # remove white spaces.
if not line.strip():  # skip blank lines.
continue
if clean_line[:3] == 'Pat':  # if line is path.
my_val = clean_line.split(':')[1].strip()
if clean_line[:3] == 'Com':  # if line is components.
my_key = clean_line.split(':')[1].strip()
result_list.append({my_key:my_val})
print(result_list)

相关内容

  • 没有找到相关文章

最新更新