首次写入时覆盖文本文件,然后附加到它 - Python

时间:2014-07-12 21:11:29

标签: python-3.x output

我对该文件的第一次写入需要覆盖它,然后我的下一个需要附加到它。但是没有办法知道首先写什么。我的写作是在条件语句中。这就是我所拥有的:

class MyHTMLParser(HTMLParser):
    def __init__(self):
        HTMLParser.__init__(self)
        self.strict = False
        self.indent = " "
        self.pos = 0
        self.output_file = 'output_sass.txt'

    def handle_starttag(self, tag, attrs):
        if attrs != []:
            for attr in attrs:
                if ('id' in attr):
                    id = attr.index('id')
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + '#' + attr[id+1] + ' {' +'\n')
##                    print (self.indent * self.getpos()[1] + "#" + attr[id+1] + " {")
                    self.pos = self.getpos()[1]
                    break
                elif ('class' in attr):
                    clas = attr.index('class')
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + "." + attr[clas+1] + " {"+'\n')
##                    print (self.indent * self.getpos()[1] + "." + attr[clas+1] + " {")
                    self.pos = self.getpos()[1]
                    break
                else:
                    with open(self.output_file, 'w') as the_file:
                        the_file.writelines(self.indent * self.getpos()[1] + tag + " {"+'\n')
##                    print (self.indent * self.getpos()[1] + tag + " {")
                    self.pos = self.getpos()[1]
                    break
        else:
           with open(self.output_file, 'w') as the_file:
                the_file.writelines(self.indent * self.getpos()[1] + tag + " {"+'\n')
##            print (self.indent * self.getpos()[1] + tag + " {")
                self.pos = self.getpos()[1]

    def handle_endtag(self, tag):
        with open(self.output_file, 'w') as the_file:
            the_file.writelines(self.indent * self.pos + "}"+'\n')
##        print(self.indent * self.pos + "}")

2 个答案:

答案 0 :(得分:2)

添加一个包含更改标志的class属性:

import itertools

class MyHTMLParser(HTMLParser):
    def __init__(self, ...):
        ...
        self.modes = itertools.chain('w', itertools.cycle('a'))

    @property
    def mode(self):
        return next(self.modes)

    def handle_starttag:
        ...
        with open(filepath, self.mode) as the_file:  # self.mode is 'w' the first time and 'a' every time thereafter
            # write stuff

答案 1 :(得分:1)

使用一些变量first_write = True并在所有地方检查它。然后将其更改为False

或者将数据放在某个列表中,最后只写一次。

当然你可以在开头写一次文件(删除上一个文件)并在结尾处关闭它。

__init__执行self.the_file = open(self.output_file, 'w')并且您已打开文件,您可以在所有课程中访问它。我不知道何时会结束程序以关闭文件self.the_file.close()。也许HTMLParser在数据末尾调用了一些函数。

请参阅HTMLParser.close() - 这似乎是关闭文件的好地方。您将不得不覆盖它,并可能从原始类close()调用HTMLParser