Dragoș Iorgulescu's website

Home \ Blog \ Discard content from a text file (with Python)
  
🗓  

Recently I came across a scenario where I needed to process a text file so that a certain portion at the beginning was to be truncated up to a certain given line.

After doing a bit of digging, I came across a very elegant solution on StackOverflow which involved the use of itertools & lambda statements.

Say, for example, that we have the following text:

> Sed posuere urna vel risus scelerisque, sit amet sollicitudin arcu porta. Vestibulum nec posuere erat. Duis nec finibus magna. Phasellus efficitur tempus lacus suscipit tincidunt. Sed efficitur lacinia arcu, ac ullamcorper nisi consectetur eget. > Aliquam nisl eros, tincidunt non commodo ac, lobortis vel quam. Morbi ac magna in sapien imperdiet aliquet nec nec augue. Mauris ut sollicitudin dolor, ut venenatis sapien. Morbi nunc eros, sodales eget justo vel, suscipit porttitor sapien. > THIS IS A DELIMITER > > Ut non eros purus. In fringilla ex arcu, eu vestibulum dui suscipit ac. Duis nec sagittis est. Praesent fringilla metus et congue placerat. Nulla viverra, purus quis mollis mollis, risus tortor interdum ante, id congue sem purus a orci. Vestibulum ante mi, pretium sollicitudin turpis vitae, malesuada finibus velit. Duis feugiat, urna a elementum ullamcorper, nulla felis pretium risus, sed porttitor metus dolor nec purus.

Now, we have the following code:

from itertools import dropwhile import os import sys with open('path/to/your/text/file', 'r') as file:     # Provide the text that marks the end of the block     #which will be truncated - in this example, it's     # "THIS IS A DELIMITER"     dropped = dropwhile(lambda         _line: "THIS IS A DELIMITER" not in _line, file)     next(dropped,"")     for line in dropped:     # You decide what to do with the remaining lines.     # In this example, we just print them out.     # Of course, these can be "forwarded" to another     # file handle     print(line)

The outcome is a text stripped of all content up to (and including) the supplied "delimiter" (in this example, **THIS IS A DELIMITER**)

Hits

Valid CSS! HTML5 Valid Donate using PayPal
GDPR Info | Built with Jekyll | Hosted on GitHub Pages with FreeDNS.afraid.org | Subscribe via RSS