简体   繁体   English

读取文件的下一行以确定是否打印当前行

[英]Read next lines of a file to determine whether or not to print current line

I'm trying to write this program slicer program in python, but this little bit is taking me forever to figure out.我正在尝试在 python 中编写这个程序切片器程序,但这一点点让我永远想明白。

I want to read certain file line by line and only print the lines that affect a certain variable.我想逐行读取某些文件,并且只打印影响某个变量的行。 In this case num在这种情况num

this is the text file I'm reading from:这是我正在读取的文本文件:

a = 3
b = 4
num = 0

while a > b:
    if a > b:
        a = a +2
        b = b + 1

        if a > 2:
            num-=1

My python file that has to read this file ^ and it has to isolate the variable num by printing the lines that contain the variable num and the loop statements where num is inside.我的 python 文件必须读取此文件 ^ 并且它必须通过打印包含变量num的行和其中num所在的循环语句来隔离变量num This is the output I expect:这是我期望的 output:

num = 0

while a > b:
    if a > b:
        if a > 2:
            num-=1

This is my code so far and the part of the code that is in charge of reading the lines inside the loops is not working:到目前为止,这是我的代码,负责读取循环内行的代码部分不起作用:

# Open file
    file = open('text.py', 'r')
    Lines = file.readlines()
    count = 0
    word = "num"
    # Store all the loop statements that can appear in the file
    keyword = ["while", "for", "if", "else", "elif", "def"]

    for i in range(len(Lines)):
        count += 1

        # if line contains keyword
        if any(word in Lines[i] for word in keyword):
            # read lines inside the loop
            # count_indent is a function that counts the indent of the line to compare it and see if the line is inside the loop or not. I didn't added to the question shorter
            line_indent = count_indent(Lines[i])+4
            while i <= len(Lines) and count_indent(Lines[i + 1]) == line_indent:

                # if word appears in the next lines that are inside the loop then we print the loop statement
                if word in Lines[i + 1]:
                    print(str(count) + " : " + Lines[i])

                else:
                    break

                # move to next line inside the loop
                i = i + 1


        # If line is not empty and contains word
        if Lines[i] != '\n' and word in Lines[i]:
            print(str(count) + " : " + Lines[i])

This code output is similar to what you expect.此代码 output 与您的预期相似。

file = open('text.py', 'r', newline = '\n')
lines = file.readlines()
word = "num"
keyword = ["while", "for", "if", "else", "elif", "def"]

with open('out_text.py', 'wt') as fout:
    
    for line in lines:

        if any(word in line for word in keyword):

            fout.write(line + '\n')

        elif word in line:

            fout.write(line + '\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python“for line in file”,读取接下来的n行 - Python "for line in file", read the next n lines 如果当前行包含字符串,如何从文件中打印下一行? - How to print the next line from a file, if current line contains a string? 在逐行读取文件的同时读取下一行和上一行 - Read next and previous lines while reading file line-by-line 从文本文件中读取行,如果特定单词 == 100,则打印该行 - Read lines from text file and if a specific word == 100, print the line Python 可以使用 \n 从文件中读取行并将其打印为 2 行吗? - Can Python read line from file with \n and print it in 2 lines? 逐行阅读并获取上一行和下一行 - Read line by line and get the previous and the next lines 阅读 python 文件中的下 3 行 - Read the 3 next lines in python file 打开一个txt文件,读取一行,最后将其标记为“已发送”。 在下一次迭代中,读取未标记的行 - Open a txt file, read a line, tag it at the end as 'Sent'. In next iteration, read lines which are untagged 从嵌套字典中的文件中读取最初未知数量的N行,然后在下一次迭代的N + 1行开始 - Read initially unknown number of N lines from file in a nested dictionary and start in next iteration at line N+1 如何读取文本文件中的关键字和下一行直到 python 中的空白行 - how to read a keyword in text file and the next lines till a blank line in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM