1.将2018-08-06.jog中的日志修改成字典以达到预期效果
#注:文件必须在一个文件内,如果不在需要另一种语法
lst = []
with open("2018-08-06.jog",mode="r",encoding="utf-8") as f:
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("2018-08-06.jog",mode="r",encoding="utf-8") as f:
first = f.readline().strip().split(',')
for line in f:
dic = {}
ls = line.strip().split(',')
for i in range(len(first)):
dic[first[i]] = ls[i]
lst.append(dic)
print(lst)