-1

My input file test.sas file looks like below:

My    
Name is Joe
How are you?;
/* Comment 1 */
/*Comment 2 
blah blah
blah blah blah
blah blah blah blah;*/
/* Comment 3 */
I am great

When I use the following code I get the result that is right after the code:

writing = True

with open('test.sas', 'r') as file:
    with open('output.sas','w') as out:
        for line in file:
            if writing:
                if '/*' in line:
                    writing = False
                else:
                    out.write(line)
            elif '*/' in line:
                writing = True

the result:

My    
Name is Joe
How are you?;

But I would like to get the following result:

My    
Name is Joe
How are you?;
I am great

I can't figure out what I am doing wrong. Basically I want to remove the text within the comment block and write the rest to the output file.

1
  • 3
    You can only detect /* OR */ on a line because of your elif (not lines with both). Change that to an if Commented Jul 31, 2018 at 13:34

3 Answers 3

3

Change your final

        elif '*/' in line:
            writing = True

to

        if '*/' in line:
            writing = True

That way you can hit both statements if they both exist on the same line.

Sign up to request clarification or add additional context in comments.

Comments

0

I've added some comments showing the status of your writing variable at the end of each line.

My                     //True
Name is Joe            //True
How are you?;          //True
/* Comment 1 */        //False
/*Comment 2            //False
blah blah              //False
blah blah blah         //False
blah blah blah blah;*/ //True
/* Comment 3 */        //False
I am great             //False

Do you see where the problem comes in? Since the if-statement can is only evaluated once per line (once per loop), it does not set writing to True after lines that have both the /* and */.

To check for both of the comment signs on a single line, allow both if-statements to work in one loop. Change elif '*/' in line: at the end of your program to if '*/' in line. This way, it can set to writing at the end of each loop, even if it set writing to False at first!

Comments

0

One Option may be to use regex (re.sub()) to match your quotes and substitute it with an empty string.

More on Regex

The re.sub() method is for "substituting matches" with something else

re.sub(pattern, substitute, string)

Pattern: (/*[\s\S\w\W]**/\r?\n?) - catch everything between quotes followed by maybe a return or newline

import re

with open('text.sas') as f:
    string = f.read()

pattern = r'(/\*[\s\S\w\W]*\*/\r?\n?)'

new_string = re.sub(pattern, '', string)
print(new_string)
""" Output:
My    
Name is Joe
How are you?;
I am great
"""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.