Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove comments from python file

import pyparsing

test = """
/* Code my code
xx to remove comments in C++
or C or python */

include <iostream> // Some comment

int main (){
    cout << "hello world" << std::endl; // comment
}
"""
commentFilter = pyparsing.cppStyleComment.suppress()
# To filter python style comment, use
# commentFilter = pyparsing.pythonStyleComment.suppress()
# To filter C style comment, use
# commentFilter = pyparsing.cStyleComment.suppress()

newtest = commentFilter.transformString(test)
print(newtest)
Comment

python program to remove comment lines

# reading the file
with open("oldfile.py") as fp:
    contents=fp.readlines()

# initialize two counter to check mismatch between "(" and ")"
open_bracket_counter=0
close_bracket_counter=0 

# whenever an element deleted from the list length of the list will be decreased
decreasing_counter=0   

for number in range(len(contents)):

    # checking if the line contains "#" or not
    if "#" in contents[number-decreasing_counter]:

        # delete the line if startswith "#"
        if contents[number-decreasing_counter].startswith("#"):
            contents.remove(contents[number-decreasing_counter])
            decreasing_counter+=1

        # delete the character after the "#"    
        else:  
            newline=""  
            for character in contents[number-decreasing_counter]:
                if character=="(":
                    open_bracket_counter+=1
                    newline+=character
                elif character==")":
                    close_bracket_counter+=1
                    newline+=character
                elif character=="#" and open_bracket_counter==close_bracket_counter:
                    break
                else:
                    newline+=character
            contents.remove(contents[number-decreasing_counter])     
            contents.insert(number-decreasing_counter,newline)   


# writing into a new file
with open("newfile.py","w") as fp:
    fp.writelines(contents)
Comment

PREVIOUS NEXT
Code Example
Python :: python divide floor 
Python :: python import worldcloud 
Python :: pandas subtract days from date 
Python :: random.shuffle 
Python :: django change user password 
Python :: python change column order in dataframe 
Python :: how to close a webpage using selenium driver python 
Python :: rename key in dict python 
Python :: spacy ner 
Python :: sparse categorical cross entropy python 
Python :: flask read form data 
Python :: ternary operator python 
Python :: html to docx python 
Python :: model o weight 
Python :: numpy matrix 
Python :: numpy flatten 
Python :: python while not 
Python :: check pygame version 
Python :: python numpy array delete multiple columns 
Python :: pandas convert series of datetime to date 
Python :: Django group by date from datetime field 
Python :: concatenate data vertically python 
Python :: count lines in file python 
Python :: capitalise words in a column pandas 
Python :: keras.layers.MaxPool2D 
Python :: python list empty 
Python :: what does ^ do python 
Python :: python get first day of year 
Python :: how to print two lists side by side in python 
Python :: how to make a pause in python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =