You can read the text file word by word! :)
d = {'flight':[]}
f = #text file
for line in f:
for word in line.split():
if word == what_you_want:
d['flight'].append(word)
Hows that? Im a bit confused by exactly what you want but that will read a text file by each word. Replace the if statement with your own one that does something more relevant.
Paul Thompson
Veteran Poster
1,119 posts since May 2008
Reputation Points: 264
Solved Threads: 183
If I understand you right, it should look like this ...
# assumed test data (text from a data file)
data_str = """\
T34712 ABERDEEN 0800 LANDED 08:00
BE171 SOUTHAMPTON 0820 LANDED 08:07"""
# first create a list of lists
data_list = [line.split() for line in data_str.split('\n')]
print data_list
"""
my prettied up result -->
[
['T34712', 'ABERDEEN', '0800', 'LANDED', '08:00'],
['BE171', 'SOUTHAMPTON', '0820', 'LANDED', '08:07']
]
"""
# now create a list of dictionaries
dict_list = []
data_dict = {}
for line in data_list:
sf = "%s, From:%s, scheduled %s, remark %s"
value = sf % (line[0], line[1], line[2], line[3])
data_dict['flight'] = value
dict_list.append(data_dict)
# start dictionary over
data_dict = {}
print(dict_list)
"""
my prettied up result -->
[
{'flight': 'T34712, From:ABERDEEN, scheduled 0800, remark LANDED'},
{'flight': 'BE171, From:SOUTHAMPTON, scheduled 0820, remark LANDED'}
]
"""
vegaseat
DaniWeb's Hypocrite
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417