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 :: google.api_core.exceptions.ServiceUnavailable: 503 The datastore operation timed out, or the data was temporarily unavailable when using stream 
Python :: python class optional arguments 
Python :: add function name and line number in python log file 
Python :: python slicing string 
Python :: checking if something is true. infinite 
Python :: python for infinite range 
Python :: deck of cards exercise in python 
Python :: csv file python 
Python :: How to Remove Items in a Set in Python Using the remove() Method 
Python :: using return values python script in batch script 
Python :: reverse bolean python 
Python :: method 01 of making GUI with tkinter in python 
Python :: numerical columns 
Python :: design patterns python - restrict what methods of the wrapped class to expose 
Python :: how to use print function in python stack overflow 
Python :: isclass function in python xml 
Python :: python async get result 
Python :: convert set to list python time complexity method 2 
Python :: python rest api interview questions 
Python :: Lightbank b2c 
Python :: azureservicebus legacy-install-failure 
Python :: sort python dictionary with values of list by index of first one 
Python :: eastcoders: django-meta-class 
Python :: python round function 
Python :: preventing players from changing existing entries in tic tac toe game 
Python :: how to run ewa requirement.txt file 
Python :: tqdm continues afer break 
Python :: scatter plot actual vs predicted python 
Python :: python-wordpress-xmlrpc get post id 
Python :: plt.savefig no frame 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =