Delete lines containing a specific word

Martin Marcher martin at marcher.name
Sun Jan 6 16:42:47 EST 2008


On Sunday 06 January 2008 21:25 Francesco Pietra wrote:
>> yes lines starting with a "#" are comments in python but that shouldn't
>> be of concern for your input data. I don't quite get what you want
>> here...
> 
> Leaving the lines commented out would permit to resume them or at least
> remeber what was suppressed. During trial and error set up of a
> calculation one never knows exactly the outcome

Ah I get it.

To clarify:
The character python uses as a comment has nothing to do with the character
your data file uses as a comment.

So you could of course use the "#" sign (which makes sense)
You could also use "//" (C-Style) or whatever you like

class CommentHelper(object):
        """Provides the necessary
        methods to comment or uncomment a line of text.
        """
        # Yes I know this docstring is badly formatted but
        # I thought it's nicer to keep the indentation.
        def __init__(self, commentStr=None):
                if commentStr:
                        self.commentStr = commentStr
                else:
                        self.commentStr = "MY_SUPER_COMMENT_STRING"
        def commentLine(line):
                """Comments a line with the
                initialized comment string.
                """
                return self.commentStr + " " + line
        def uncommentLine(line):
                """Uncomments a line iff it is
                preceded by the comment string.
                """
                if line.startsWith(self.commentStr):
                        return line[len(self.commentStr)].lstrip()
                raise Exception("Can't uncomment Line with no comment")
                        

You want to read up about:
        * (new style) classes
        * parameters with default values
        * docstrings
        * Exceptions


That code is untested and may contain errors. I'll let the debugging be your
task :)

hope it points you to the right topics to read up about:
http://docs.python.org/
http://docs.python.org/tut/tut.html
http://www.diveintopython.org/

martin

-- 
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours




More information about the Python-list mailing list