simplest way to strip a comment from the end of a line?
Arnaud Delobelle
arnodel at googlemail.com
Thu Dec 4 12:14:17 EST 2008
Joe Strout <joe at strout.net> writes:
> I have lines in a config file which can end with a comment (delimited
> by # as in Python), but which may also contain string literals
> (delimited by double quotes). A comment delimiter within a string
> literal doesn't count. Is there any easy way to strip off such a
> comment, or do I need to use a loop to find each # and then count the
> quotation marks to its left?
>
> Thanks,
> - Joe
FWIW this is what comes to mind.
>>> def strip_comment(line):
... i = -1
... while True:
... i = line.find('#', i+1)
... if i == -1:
... return line
... if line.count('"', 0, i) % 2 == 0:
... return line[:i]
...
>>> strip_comment('foo=1 # set foo')
'foo=1 '
>>> strip_comment('foo="bar" # set foo')
'foo="bar" '
>>> strip_comment('foo="bar # set foo"')
'foo="bar # set foo"'
>>> strip_comment('foo="bar # set foo" # set foo')
'foo="bar # set foo" '
>>> strip_comment('foo="bar # set foo" + "baz ## fubar" # set foo')
'foo="bar # set foo" + "baz ## fubar" '
>>> strip_comment('foo="bar # set foo" + "baz ## fubar # set foo"')
'foo="bar # set foo" + "baz ## fubar # set foo"'
>>> strip_comment(r'foo="bar\" baz" # this breaks')
'foo="bar\\" baz" # this breaks'
As the last example shows, it won't work if there is an escaped double
quote in the string.
--
Arnaud
More information about the Python-list
mailing list