Hi folks

Can anyone explain to me why this is not working please?

I am trying to delete the first 52 lines of a text file, then skip deleting the next line, then deleting the next 100 lines, then skipping the next line and repeating the process until the file ends (requires about 100 loops), i.e. delete lines 0-51, 53-153, 155-255, 257-357 and so on about 100 hundred times.

All the lines that remain, which are what I need, are then written to a new file.

When I run this only the first 52 lines are deleted, and then nothing more happens... the script just stops. Is it because readlines is unable to hold all the data? There are about 10,000 lines in the text file!! I am VERY new to python and don't have any clue how else to do this really..

#!/usr/bin/env python

# read the data file in as a list
fin = open("/Users/stuartmuller/Sort/PhD/Analytical testing/Benchmark10/10a1_1/BM10a1_1/LCONR1_OUT.DAT", "r" )
lines_list = fin.readlines()
fin.close()

begin = 0
end = 51
count = 0
while (count < 101):
    del lines_list[begin:end]
    begin = end + 2
    end = end + 100
    count=count+1

# Write the updated list of lines to a new file
fout = open("/Users/stuartmuller/Sort/PhD/Analytical testing/Benchmark10/10a1_1/BM10a1_1/LCONR1_BTC.DAT", "w")
fout.writelines(lines_list)
fout.close()

You can use a code structure like this:

next_number = 52
for i, line in enumerate(fin):
    if i == next_number:
        fout.write(line)
        next_number += 102

I tried it with fewer lines and get the same effect - the first delete works but nothing thereafter.

I should add that each line contains 101 elements! So the total file has about 10,000 lines with each line containing 101 values (not that the values within each line need to be stored separately - I need to delete and keep entire lines)

You can use a code structure like this:

next_number = 52
for i, line in enumerate(fin):
    if i == next_number:
        fout.write(line)
        next_number += 102

That is exactly what I was looking for! THANK YOU!

Not only a solution, but also an education - much 'bliged :-)

I tried it with fewer lines and get the same effect - the first delete works but nothing thereafter.

I should add that each line contains 101 elements! So the total file has about 10,000 lines with each line containing 101 values (not that the values within each line need to be stored separately - I need to delete and keep entire lines)

I suggest you create a cpy of orignal and delete what you want and store that list, then take a copy of orignal modify the lines you need etc. I suggest that because deleting stuffs changes the indexing of list.

EDIT:
you got it! Bravo to G.

Edited 6 Years Ago by Stefano Mtangoo: n/a

Thanks for the help anyway :-)

I like your quote... too true. Best we can hope for is Jack of all trades, master of some, and even that is an achievement.

Thanks for the help anyway :-)

I like your quote... too true.

It comes out of experience. I tried to know everything I can and when I evaluate myself, I knew as expert nearly as nothing. So don't try that because you'll learn it the hard way ;)

Edited 6 Years Ago by Stefano Mtangoo: n/a

It comes out of experience. I tried to know everything I can and when I evaluate myself, I knew as expert nearly as nothing. So don't try that because you'll learn it the hard way ;)

I am essentially subject to unbounded interest in pretty much anything and everything - no matter what I look at or get into, it seems there is a lifetime's worth of exploration to be had...

An infinite amount to learn and experience about an infinite number of subjects and activities. The risk is not so much a failure to gain expertise as much as it is a failure to retain sanity!

Stupid infinitely awesome universe ;-)

You can test Gribouillis' clever code with something like this ...

# delete lines 0-51, 53-153, 155-255, 257-357 and so on

in_list = list(range(1000))

out_list = []
save_line = 52
for ix, line in enumerate(in_list):
    if ix == save_line:
        out_list.append(line)
        save_line += 102

print(out_list)  # [52, 154, 256, 358, 460, 562, 664, 766, 868, 970]

I am essentially subject to unbounded interest in pretty much anything and everything - no matter what I look at or get into, it seems there is a lifetime's worth of exploration to be had...

An infinite amount to learn and experience about an infinite number of subjects and activities. The risk is not so much a failure to gain expertise as much as it is a failure to retain sanity!

Stupid infinitely awesome universe ;-)

Sure, we learn until we die. We learn a lot of varieties. But you need one field of expertise. You cant be expert in everything. The only human who have been expert in everything is Mr. Expert'n nothing ;)

This question has already been answered. Start a new discussion instead.