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.
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