1.11M Members

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.47KB)
 
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