如何在 python 中将文本文件解析为字典,一行上跟两行值的键



我有一个文件,其中包含以下格式的行:

CALSPHERE 1             
1 00900U 64063C   20161.15561498  .00000210  00000-0  21550-3 0  9996
2 00900  90.1544  28.2623 0029666  80.8701  43.4270 13.73380512769319
CALSPHERE 2             
1 00902U 64063E   20161.16836122  .00000025  00000-0  23933-4 0  9990
2 00902  90.1649  30.9038 0019837 126.9344   3.6737 13.52683749559421

..等。

我想将其解析为格式的字典:

{CALSPHERE 1:(1 00900U 64063C   20161.15561498  .00000210  00000-0  21550-3 0  9996, 2 00900  90.1544  28.2623 0029666  80.8701  43.4270 13.73380512769319),
CALSPHERE 2:(1 00902U 64063E   20161.16836122  .00000025  00000-0  23933-4 0  9990, 2 00902  90.1649  30.9038 0019837 126.9344   3.6737 13.52683749559421),...}

我对如何解析它感到困惑,因此每三行都是键,以下两行形成值的元组。在 python 中执行此操作的最佳方法是什么?

我试图为"每三行"添加一些逻辑,尽管它似乎有点复杂;

with open(r"file") as f:
i = 3
for line in f:
if i%3=0:
key = line
else:
#not sure what to do with the next lines here

如果你的文件总是具有相同的分布(即:"CALSPHERE"单词 - 或任何其他你想要它作为字典键的单词 - 后跟两行(,你可以通过执行以下操作来实现你想要的:

with open(filename) as file:
lines = file.read().splitlines()
d = dict()
for i in range(0, len(lines), 3):
d[lines[i].strip()] = (lines[i + 1], lines[i + 2])

输出:

{
'CALSPHERE 1': ('1 00900U 64063C   20161.15561498  .00000210  00000-0  21550-3 0  9996', '2 00900  90.1544  28.2623 0029666  80.8701  43.4270 13.73380512769319'),
'CALSPHERE 2': ('1 00902U 64063E   20161.16836122  .00000025  00000-0  23933-4 0  9990', '2 00902  90.1649  30.9038 0019837 126.9344   3.6737 13.52683749559421')
}

假设您的内容在文件中.txt您可以使用以下内容。 它适用于任意数量的 CALSPHERE 关键字出现以及它们之间的各种数量的条目。

with open('file.txt') as inp:
buffer = []
for line in inp: 
# remove newline
copy = line.replace('n','')
# check if next entry
if 'CALSPHERE' in copy:
buffer.append([]) 
# add line
buffer[-1].append(copy)
# put the output into dictionary
res = {}
for chunk in buffer:  
# safety check
if len(chunk) > 1:
res[chunk[0]] = tuple( chunk[1:] ) 
print(res)

最新更新