How to remove '#' comments from a string?

16,145

Solution 1

You could achieve this through re.sub function.

import re
def stripComments(code):
    code = str(code)
    return re.sub(r'(?m)^ *#.*\n?', '', code)

print(stripComments("""#foo bar
bar foo
# buz"""))

(?m) enables the multiline mode. ^ asserts that we are at the start. <space>*# matches the character # at the start with or without preceding spaces. .* matches all the following characters except line breaks. Replacing those matched characters with empty string will give you the string with comment lines deleted.

Solution 2

def remove_comments(filename1, filename2):
    """ Remove all comments beginning with # from filename1 and writes
    the result to filename2
    """

    with open(filename1, 'r') as f:
        lines = f.readlines()

    with open(filename2, 'w') as f:
        for line in lines:
            # Keep the Shebang line
            if line[0:2] == "#!":
                f.writelines(line)
            # Also keep existing empty lines
            elif not line.strip():
                f.writelines(line)
            # But remove comments from other lines
            else:
                line = line.split('#')
                stripped_string = line[0].rstrip()
                # Write the line only if the comment was after the code.
                # Discard lines that only contain comments.
                if stripped_string:
                    f.writelines(stripped_string)
                    f.writelines('\n')

Share:
16,145

Related videos on Youtube

Destiny Brown
Author by

Destiny Brown

Updated on June 04, 2022

Comments

  • Destiny Brown
    Destiny Brown almost 2 years

    The problem: Implement a Python function called stripComments(code) where code is a parameter that takes a string containing the Python code. The function stripComments() returns the code with all comments removed.

    I have:

    def stripComments(code):
       code = str(code)
       for line in code:
           comments = [word[1:] for word in code.split() if word[0] == '#']
           del(comments)
    stripComments(code)
    

    I'm not sure how to specifically tell python to search through each line of the string and when it finds a hashtag, to delete the rest of the line. Please help. :(

    • Avinash Raj
      Avinash Raj about 9 years
      an example would be better.
    • Destiny Brown
      Destiny Brown about 9 years
      I wasn't given an example... And i'm not sure how it is supposed to look.
    • Asclepius
      Asclepius over 4 years
      Consider distutils.text_file.TextFile(file=io.StringIO(code)).readlin‌​es() which uses TextFile with StringIO as needed.
  • rerx
    rerx over 5 years
    Note that this will not remove comments at the ends of active code lines.
  • Bastien
    Bastien almost 4 years
    note if a line of code were to contain '#' as part of the code this would also not work even if you fixed it to work after active code lines by removing the ^ from the regex string
  • Bastien
    Bastien almost 4 years
    This would end up badly if # was contained in a string as part of the code