简体   繁体   中英

Writing lines to new file from existing file

I have a file with close to 5000 lines (il3). I want to write the first 7 lines and then a certain number of lines at the end of the file to a new file (il3NEW). I am able to write the first 7 lines, but when I try to write the later lines I am not finding anything that works.

Not all files I will be working with are identical so I do not simply want to look at the file and pick line numbers and write, I need to write lines from the original file 'il3', to the new file 'il3NEW', starting at a specific line where a certain word is mentioned then all the way to the end of the file.

I have successfully opened 'il3' as well as a blank new file (il3NEW). I used a for loop to write the first 7 lines. When I try to write based on the location of a certain word in 'il3', I do not know how to identify that line number and then use that line number to write the lines from there to the end of the file to the 'il3NEW'.

This is what I have that works:

il3= open("il3dta",'r')

il3NEW= open("il3dtaNEW",'w')

for i in range(7):
    m=il3.readline()
    il3NEW.write(m)

il3.close()
il3NEW.close()

I also have the following that adds the immediate line:

for i in range(5000):
    m=il3.readline()
    if m == ' -1    TREE  POINTS(=1),OTHERWISE NO TREE\n':
        il3NEW.write(m)

However, I want all following lines as well. How do I get it to loop through all lines after and write those as well?

I also noticed that whenever I write anything before the for loop and between the open statement, the for loop no longer works. For example, if I try to define some variables:

#lines=il3.readlines()
#searchquery = ' -1    TREE  POINTS(=1),OTHERWISE NO TREE\n'

The for loops no longer work and my new file is empty.

It would also be nice if the second loop I have for the later lines were to loop through the entire file without having to write 5000. I tried:

for i in range(len(il3.readlines(  ))):
    m=il3.readline()
    if m == ' -1    TREE  POINTS(=1),OTHERWISE NO TREE\n':
        il3NEW.write(m)

But that does not work. If I try to define the number of lines as above, it results in a blank file. Any help in fixing this would be much appreciated! I am new to Python so I apologize for the basic question, but I couldn't find questions with the same issues. Thanks!!

If I understand correctly you can modify your approach a bit to have a flag HaveFoundTheLineImLookingFor which is True if you found your special line. Then, if this is true, we write every line with an added or to your if statement:

HaveFoundTheLineImLookingFor = False
for i in range(5000):
    m=il3.readline()
    if (m == ' -1    TREE  POINTS(=1),OTHERWISE NO TREE\n'  or HaveFoundTheLineImLookingFor ):
        il3NEW.write(m)
        HaveFoundTheLineImLookingFor  = True

An even better approach would be to not limit ourselves to files with 5000 lines:

HaveFoundTheLineImLookingFor = False
with open('il3dta') as MyFile:
    for i, line in enumerate(MyFile):
        if(i < 7):
            il3NEW.write(line)
        if(HaveFoundTheLineImLookingFor  ):
            il3NEW.write(line)
        if(line == ' -1    TREE  POINTS(=1),OTHERWISE NO TREE\n' and !HaveFoundTheLineImLookingFor  ):
            il3NEW.write(line)
            HaveFoundTheLineImLookingFor  = True

Please make a habit of using context managers with files.

with open('il3dra') as il3, open('il3dtaNEW', 'w') as il3_new:
    found = False
    for line_idx , line in enumerate(il3):
        if line.strip() == ' -1    TREE  POINTS(=1),OTHERWISE NO TREE':
            found = True

        if found or line_idx < 7:
            il3_new.write(line)

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