Extract specific lines from file and create sections of data in python

后端 未结 3 2113
再見小時候
再見小時候 2021-01-21 15:31

Trying to write a python script to extract lines from a file. The file is a text file which is a dump of python suds output.

I want to:

  1. strip all charac
3条回答
  •  独厮守ぢ
    2021-01-21 15:50

    If you want to extract the specific number of lines after a specific line that matches. You may as well simply read in the array with readlines, loop through it to find the match, then take the next N lines from the array too. Also, you could use a while loop along with readline, which is preferable if the files can get large.

    The following is the most straight-forward fix to your code I can think of, but its not necessarily the best overall implementation, I suggest following my tips above unless you have good reasons not to or just want to get the job done asap by hook or crook ;)

    newlines = []
    for i in range(len(linelist)):
        mylines = linelist[i].split()
        if re.search(r'\w+', 'ArrayOf_xsd_string'):
            for l in linelist[i+2:i+20]:
                newlines.append(l)
            print newlines
    

    Should do what you want if I have interpreted your requirements properly. This says: take the next but one line, and the next 17 lines (so, up to but not including the 20th line after the match), append them to newlines (you cannot append a whole list at once, that list becomes a single index in the list you are adding them to).

    Have fun and good luck :)

提交回复
热议问题