假设我有一个包含以下内容的文本文件:
line = "this is line 1" line2 = "this is the second line" line3 = "here is another line" line4 = "yet another line!"
我想将它们快速转换为字典键/值,并以“ line *”作为键,将引号中的文本作为值,同时还要删除等号。
用Python做到这一点的最佳方法是什么?
f = open(filepath, 'r') answer = {} for line in f: k, v = line.strip().split('=') answer[k.strip()] = v.strip() f.close()
希望这可以帮助