An homage to generators:
#!/usr/bin/env python
data=(zip(*([elt.strip().title() for elt in line.replace(':',',',1).split(',')]
for line in open('filename.txt','r'))))
personal_list=[dict(zip(data[0],datum)) for datum in data[1:]]
print(personal_list)
# [{'Food': 'Pizza', 'Car': 'Db9', 'Name': 'John'}, {'Food': 'Lasagne', 'Car': 'M5', 'Name': 'Jane'}]
To understand how the script works, we break it apart:
First we load filename.txt into a list of lines:
In [41]: [line for line in open('filename.txt','r')]
Out[41]: ['name: john, jane\n', 'car: db9, m5\n', 'food: pizza, lasagne\n']
Next we replace the first colon (:) with a comma (,)
In [42]: [line.replace(':',',',1) for line in open('filename.txt','r')]
Out[42]: ['name, john, jane\n', 'car, db9, m5\n', 'food, pizza, lasagne\n']
Then we split each line on commas:
In [43]: [line.replace(':',',',1).split(',') for line in open('filename.txt','r')]
Out[43]:
[['name', ' john', ' jane\n'],
['car', ' db9', ' m5\n'],
['food', ' pizza', ' lasagne\n']]
For each element in each line, we strip off beginning/ending whitespace and capitalize the string like a title:
In [45]: [[elt.strip().title() for elt in line.replace(':',',',1).split(',')] for line in open('filename.txt','r')]
Out[45]: [['Name', 'John', 'Jane'], ['Car', 'Db9', 'M5'], ['Food', 'Pizza', 'Lasagne']]
Now we collect the first element of each list, then the second, and so forth:
In [47]: data=(zip(*([elt.strip().title() for elt in line.replace(':',',',1).split(',')] for line in open('filename.txt','r'))))
In [48]: data
Out[48]: [('Name', 'Car', 'Food'), ('John', 'Db9', 'Pizza'), ('Jane', 'M5', 'Lasagne')]
data[0] now holds the keys for a dict.
In [49]: data[0]
Out[49]: ('Name', 'Car', 'Food')
Each tuple in data[1:] are the values for a dict.
In [50]: data[1:]
Out[50]: [('John', 'Db9', 'Pizza'), ('Jane', 'M5', 'Lasagne')]
Here we zip up the keys with the values:
In [52]: [ zip(data[0],datum) for datum in data[1:]]
Out[52]:
[[('Name', 'John'), ('Car', 'Db9'), ('Food', 'Pizza')],
[('Name', 'Jane'), ('Car', 'M5'), ('Food', 'Lasagne')]]
Finally, we turn it into a list of dicts:
In [54]: [dict(zip(data[0],datum)) for datum in data[1:]]
Out[54]:
[{'Car': 'Db9', 'Food': 'Pizza', 'Name': 'John'},
{'Car': 'M5', 'Food': 'Lasagne', 'Name': 'Jane'}]