text file to dictionary

 
0
 

test.txt

ok i want to read this file into a dictionary and this is the output
d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
etc

flight is the key

do i have to parse the text file i know i cant put it straight into the dictionary as i get this output {flightfromscheduledremark: whole file}
if i have to parse it can anyone give me a couple of links please.

or can i read the text file word by word and use a while loop like

while (not end of file)
if word is first in sentance
d.append{flight:test}

heres my code currently if anyone wants to look and criticise.

!/usr/bin/env python

import HTMLParser
class MyParser(HTMLParser.HTMLParser):

########################################################
def __init__(self):
    HTMLParser.HTMLParser.__init__(self)
    self.titleFound = False
    return

########################################################
def handle_starttag(self, tag, attrs)

    if tag == 'td':

        self.titleFound = True
    return

##########################################################
def handle_data(self, titleString):

    if self.titleFound == True:
        filename = "test.csv"
        f = open(filename, 'a')
        f.write(titleString)
        f.close()

    return

##########################################################
def handle_endtag(self, tag):

    if tag == 'td':
        self.titleFound = False
    return

#############End of Class definition

if name == 'main':
titleExtractor = MyParser()
buffer = open('live.html', 'r').read()
titleExtractor.feed(buffer)

filename = "test.csv"
f = open(filename,'r')
test = f.read()

d={"flight":test}
print d

Attachments test.txt (1.47 KB)
FlightFromScheduledRemark


 T34712  ABERDEEN  0800  LANDED 08:00 


 BE171  SOUTHAMPTON  0820  LANDED 08:07 


 WOW482  NEWQUAY / BRISTOL  0835  LANDED 08:24 


 NM322  ISLE OF MAN  0850  LANDED 08:36 


 LS324  BELFAST INTL  0910  LANDED 08:54 


 BD404  EDINBURGH  0925  LANDED 09:03 


 BD291  GLASGOW  0930  LANDED 09:09 


 KL1545  AMSTERDAM  0940  LANDED 09:17 


 T34701  SOUTHAMPTON  0945  LANDED 09:33 


 BD612  BRUSSELS  0955  LANDED 09:35 


 LS202  AMSTERDAM  1010  LANDED 09:45 


 FR152  DUBLIN  1025  LANDED 10:29 


 BD412  HEATHROW  1055  LANDED 10:47 


 LS456  PARIS (CHARLES-DE-GAULLE)  1150  LANDED 12:10 


 LS286  GENEVA  1245  LANDED 12:32 


 T34714  ABERDEEN  1435   


 BD414  HEATHROW  1445   


 KL1549  AMSTERDAM  1555   


 BE731  BELFAST CITY  1620   


 T34705  SOUTHAMPTON  1710   


 NM328  ISLE OF MAN  1725   


 BD418  HEATHROW  1800   


 WOW486  PLYMOUTH / BRISTOL  1810   


 T34716  ABERDEEN  1910   


 LS232  BARCELONA  1915   


 BD406  EDINBURGH  1925   


 BD297  GLASGOW  1930   


 BD616  BRUSSELS  1930   


 FR9078  ALICANTE  1930   


 LS328  BELFAST INTL  2000   


 BE175  SOUTHAMPTON  2020   


 T34707  SOUTHAMPTON  2025   


 LS206  AMSTERDAM  2110   


 KL1543  AMSTERDAM  2120   


 LS348  DUSSELDORF  2130   


 BD420  HEATHROW  2145   


 FR156  DUBLIN  2205   Current
time: 14:03, 17 Feb 2009Last updated: 
13:59, 17 Feb 2009
 
0
 

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.

 
0
 

T34712 ABERDEEN 0800 LANDED 08:00
BE171 SOUTHAMPTON 0820 LANDED 08:07

here are 2 lines in the txt file i want to put it into a dictionary so the output is

d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}
d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}

with flights as the dictionary key

thanks for your suggestion ill try it out and reply tomorrow

 
0
 

EDIT: The below isn't too helpful as vegaseat posted a better (and full) verison.

What do you mean by

d={"flight":T34712, From:ABERDEEN, scheduled 0800, remark landed}

The other keys need to be enclosed in quotes to, so that

d['flight'] = 'T34712'
d['from'] = 'ABERDEEN'
#etc...

The best solution I think is paulthom12345's suggestion. Although, I'm still confused about the set-up you seem to want. There's a better way to organize it, such as one list with each index a separate flight, and each of those indices holding a dict with the keys for 'flight', 'from', 'scheduled', 'remark', etc. Like:

flights = [
    { 'flight': 'T34712', 'from': 'ABERDEEN', 'scheduled': 0800, 'remark': 'LANDED 08:00 ' },
    { 'flight': 'BE171', 'from': 'SOUTHAMPTON', 'scheduled': 0820, 'remark': 'LANDED 08:07 ' }
]

OR

flights = {
    'T34712': { 'from': 'ABERDEEN', 'scheduled': 0800, 'remark': 'LANDED 08:00 ' },
    'BE171': { 'from': 'SOUTHAMPTON', 'scheduled': 0820, 'remark': 'LANDED 08:07 ' }
]

using the flight number as a key in the dict, and it's value as a dict with the other info.

 
0
 

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'}
]
"""
 
0
 

sorry for the confusion guys

basically i want to read the test.txt data into a dictionary

d={"flight":BE171 , From: SOUTHAMPTON, scheduled 0820, remark landed}

where d is the dictionary

so that in future i can search thy dictionary by imputting BE171 etc hich outputs that key

does this make sense.

ive got something to work with now. ill post back when i have implemented the solution.

looks like i have to work on my making sense skills.

Question Answered as of 5 Years Ago by Paul Thompson, vegaseat and shadwickman
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article