简体   繁体   中英

How to control multiple lines by finding one keyword in python?

I am reading a python file and I want to find the line where there is a: " " " comment. After I find it I want the following lines from that python file to get some html code until the closing quotes " " " of the comment.

Python file example:

"""

Some comment in this area 

Another line with comment

Some more
More

"""

def main():
    var = something 
    file = somefile
    for i in x:

The code that I tried but isn't really working:

def main():
    file = open("pythonfile.py","r")
    infile = file.readlines()
    flag = False

    for line in infile:

        while len(line) > 0:

            if flag:
                index = line.find("\'\'\'")

                if index < 0:
                    print("<span style=\"color: green;\">",line[:],"</span>",end="")
                    flag = True
                    line = ""

                else:
                    print("<span style=\"color: green;\">",line[0:index+3],"</span>",end="")
                    flag = False
                    line = line[index+3:]  

Where it says: if index < 0: I wanted that after the program finds the quotation marks all the following lines get the span color of green until it finds the closing quotation marks.

The Output should look like this

<span style=\"color: green;\">"""</span>

<span style=\"color: green;\">Some comment in this area </span>

<span style=\"color: green;\">Another line with comment</span>

<span style=\"color: green;\">Some more</span>
<span style=\"color: green;\">More</span>

<span style=\"color: green;\">"""</span>

def main():
    var = something 
    file = somefile
    for i in x:
file = open("pythonfile.py" ,"r")
infile = file.readlines()
flag = False
# headop = True

for line in infile:
    line2=line
    if line[-1]=='\n':
        line2=line[:-1]
    index=line2.find("\"\"\"")
    if index>=0:
        index2 = line2[index+3:].find("\"\"\"")
        if index2>=0:
            index2+=(index+3)
    else:
        index2=-1
    if index<0:
        if flag:
            print("<span style=\"color: green;\">" ,line2 ,"</span>", sep="")
        else:
            print(line2)
    else:
        if flag:
            print("<span style=\"color: green;\">" ,line2[0:index+3] ,"</span>", end="", sep="")
            if index2<0:
                print(line2[index+3:])
                flag=False
            else:
                print(line2[index+3:index2], end="")
                print("<span style=\"color: green;\">" ,line2[index2:] ,"</span>", sep="")
                flag=True
        else:
            print(line2[0:index], end="")
            if index2<0:
                print("<span style=\"color: green;\">" ,line2[index:] ,"</span>", sep="")
                flag=True
            else:
                print("<span style=\"color: green;\">" ,line2[index:index2+3] ,"</span>", end="", sep="")
                print(line2[index2+3:])
                flag=False

output is

<span style="color: green;">"""</span>
<span style="color: green;"></span>
<span style="color: green;">Some comment in this area</span>
<span style="color: green;"></span>
<span style="color: green;">Another line with comment</span>
<span style="color: green;"></span>
<span style="color: green;">Some more</span>
<span style="color: green;">More</span>
<span style="color: green;"></span>
<span style="color: green;">"""</span>

def main():
    <span style="color: green;">""" this is a test</span>
<span style="color: green;">     in code block """</span>
    var = 'something'
    var2 = <span style="color: green;">"""different"""</span>
    var3 = <span style="color: green;">"""diff1"""</span>
    file = 'somefile'
    for i in range(10):
        print("i=",i)
    str = <span style="color: green;">""" this is a test</span>
<span style="color: green;">            and test and test. """</span>
    print("str=",str)

<span style="color: green;">""" call main() """</span>
main()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM