Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

for loop - Python code with a "string" input that removes #comment but not if it's a string (or it's like this 'blabla #comment blabla' it's not to be removed)

It's my first question first I have the wrong attempt that only works without weird people putting # inside a string inside a string.

Below is my attempt to do this but doesn't work. Please help me.

Is there a way to use split as it beautifully works in 2 lines without the # in "" or '' condition? Or should one just develop this while loop function bellow (last function)?... At least we would be expecting input/txt to not have both this 'string' and this "string" but if you can make it work for these mixed up and even """"strings"""" or these ''strings''. It would really help me learn as a beginner.

#def remove_cpp_com(txt):
    #    n_text=txt.split("#")
    #    return n_text[0]
    
    #This is the wrong attempt that only works without weird people putting # inside a string inside a string.
    
    #Bellow is my attempt to do this but doesn't work. Please help me.
    
    def remove_py_com(txt):
        i=0
        endproduct=""
        subgroup_number=len(txt.split("'"))
        other_subgroup_number=len(txt.split('"'))
        for i in txt:
            if i not in "'" or i not in '"':   # meaning if txt[i]!="'"  string in this string
                no_symbol_output=txt.split("#")
                endproduct+=no_symbol_output[0] 
    # no "  or ' > by splitting in '#' which removes it and returning first index should work...
                return endproduct
            elif i in'"' and other_subgroup_number>2:
                    false_string_comment=txt.split('"')
                    last_index_string=list(txt[-1])
                    tails_first_item, tails_last_item =last_index_string.split("#")[0,-1]
                    endproduct+='"'+strg(false_string_comment)+'"'+strg(tails_first_item)
                    return endproduct
            else:
                    other_false_string_comment=txt.spit("'")
                    endproduct+="'"+strg(also_false_string_comment)+"'"+strg(tails_first_item)
                    return endproduct
        return endproduct
    

#Only while loop works for this?

    def remove_py_com(txt):
        line = ""
        i = 0
        while i < len(txt):
            if txt[i] == "#":
                    return line
                line += txt[i]
                i += 1
                if txt[i-1] == '"':
                    while i < len(txt):
                    line += txt[i]
                    i += 1
                    if txt[i-1] == '"':
                        break
        return line
question from:https://stackoverflow.com/questions/65617598/python-code-with-a-string-input-that-removes-comment-but-not-if-its-a-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...