将a.txt中
序号 部门 人数 平均年龄 备注
1 python 30 26 单身狗
2 Linux 26 30 没对象
3 运营部 20 24 女生多
转化为:
[{'序号': '1', '部门': 'python', '人数': '30', '平均年龄': '26', '备注': '单身狗'}, {'序号': '2', '部门': 'Linux', '人数': '26', '平均年龄': '30', '备注': '没对象'}, {'序号': '3', '部门': '运营部', '人数': '20', '平均年龄': '24', '备注': '女生多'}]
with open("a.txt",encoding="utf-8") as f:
result_ls = [] #结果接收列表
ls = f.readline().strip().split(" ") #字典的键
for tt in f: #取后3行
dict = {} #字典一定要放在里面
ls_1 = tt.strip().split(" ")
for i in range(len(ls_1)):
dict[ls[i]] = ls_1[i]
result_ls.append(dict)
print(result_ls)