To remove all annotations

Asked 2 years ago, Updated 2 years ago, 110 views

How do I remove all annotations from the Python code?

I made a code to find a pair of # or " and erase the contents of it Occasionally, a problem occurs when the " is used as a string.

Isn't there a Python built-in function or any other external library function that does this?

python syntax blocks-comments

2022-09-22 22:22

2 Answers

As far as I know, there's no such function. But it's not that there's no way at all, and it might be possible if you use the function of the editor (such as Faicham).


2022-09-22 22:22

'In quotation marks
bound
Remove multi-line annotation ''
uncommented_code = re.sub(re.compile(r"'''.*'''",re.DOTALL ) ,"" ,_CODE) 


"""In double quotation marks
bound
Remove multi-line annotation """
uncommented_code = re.sub(re.compile(r'""".*"""',re.DOTALL ) ,"" ,uncommented_code)


Remove comments marked #
uncommented_code = re.sub(re.compile(r"#.*[\n\Z]" ) ,"\n" ,uncommented_code)

You can do it like this.


2022-09-22 22:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.