id,name,phone,car 1,小明,1001,大众 2,小马,1002,宝骏 3,小乐,1003,北汽 4,小李,1004,哈弗
# 低级版本 lst = [] with open(‘2019-05-07‘,mode=‘r‘,encoding=‘utf-8‘) as f: s = f.readline() for line in f: dic = {} # 每行一个字典 ls = line.strip().split(‘,‘) dic[‘id‘] = ls[0] dic[‘name‘] = ls[1] dic[‘phone‘] = ls[2] dic[‘car‘] = ls[3] lst.append(dic) print(lst)
# 高级版本 lst = [] with open(‘2019-05-07‘,mode=‘r‘,encoding=‘utf-8‘) as f: first_name = f.readline().strip().split(‘,‘) for line in f: dic = {} ls = line.strip().split(‘,‘) for i in range(len(first_name)): dic[first_name[i]] = ls[i] lst.append(dic) print(lst)
原文:https://www.cnblogs.com/Little-Raccoon/p/10825513.html