test.txt
Hello
basically ive parsed a website's data into an object. I wrote it to file and read it back in to write to a dictionary. but when i read it in it reads it like 3 times. so im a bit confused as to why ad would like to know what bit of codes wrong. im ne to python and am learning to debug my code but its sometimes a bit hard to find the problem as i think its all legit.
so please point out where ive gone wrong thanks. the output file itself looks fine as it writes i only once
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"
file = open(filename, 'a')
file.write(titleString)
file.close()
file = open(filename)
print file.read()
file.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)