954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

deleting data between two lines

Hi everyone,

I have a huge data file and it goes on like this.

62813,     61344,     68229,     68234,     61349,     61345,     68230   
     62814,     61345,     68230,     68235,     61350,     61346,     68231    
     62815,     61346,     68231,     68236,     61351,     61347,     68232      
     62816,     61347,     68232,     68237,     61352,     51631,     51573    
*ELEMENT,TYPE=S4,ELSET=one                                        
     62817,         1,         2,         4,         3
     62818,         3,         4,        14,        13
     62819,        13,        14,        20,        19
     62820,         2,        25,        26,         4
*ELEMENT,TYPE=S4,ELSET=two                                     
     67450,       241,       242,       244,       243
     67451,       243,       244,       246,       245
     67452,       242,       253,       254,       244
     67453,       244,       254,       255,       246

i need to delete the following lines from the file

*ELEMENT,TYPE=S4,ELSET=one                                        
     62817,         1,         2,         4,         3
     62818,         3,         4,        14,        13
     62819,        13,        14,        20,        19
     62820,         2,        25,        26,         4

so that the final output looks like this

62813,     61344,     68229,     68234,     61349,     61345,     68230   
     62814,     61345,     68230,     68235,     61350,     61346,     68231    
     62815,     61346,     68231,     68236,     61351,     61347,     68232      
     62816,     61347,     68232,     68237,     61352,     51631,     51573    
*ELEMENT,TYPE=S4,ELSET=two                                     
     67450,       241,       242,       244,       243
     67451,       243,       244,       246,       245
     67452,       242,       253,       254,       244
     67453,       244,       254,       255,       246


Thank you

kaskoraja
Newbie Poster
10 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

Do you have to delete only the first five lines ?????

richieking
Master Poster
764 posts since Jun 2009
Reputation Points: 61
Solved Threads: 152
 

How many Giga/Terabytes is this huge file? You need only to remove this specific lines, not certain element blocks?

This code drops sections that are in discard_blocks set.

discard_blocks = {'*ELEMENT,TYPE=S4,ELSET=one' }
with open('data.txt') as datain, open('data_out.txt', 'w') as dataout:
    writeout=True
    for line in datain:
        if line.rstrip() in discard_blocks:
            writeout = False
        elif line.startswith('*ELEMENT'):
            writeout = True
        if writeout:
            dataout.write(line)
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: