Remove all lines after a specific line in Python

FlyingZeppo

How would I remove all lines in a text file after a specific line?

For example, I want to change the file:

This is important line one.
This is important line two.
Here begins the throw away lines.
Here is throw away line one.
And this is throw away line two.

To this:

This is important line one.
This is important line two.

I think I would want to regex match r'Here begins the (.|\n)*' to match everything in the file after 'Here begins the', but then I'm lost.

I tried this but it's not working:

import re  
with open ("file1.txt", "r", encoding="utf-8") as f:  
    lines = f.readlines()

found_line = re.search(r'^Here begins the(.|\n)*',lines)  
lines = lines[:found_line.start()]  
with open ("file1.txt", "w", encoding="utf-8") as f:  
    f.writelines(lines)

Thank you.

Ash Nazg

You can read the file, apply the regex you mentioned with re.sub() and write to the file like this:

import re
with open("file.txt", "r") as f:
    data = f.read()
    
subbed_data = re.sub(r'Here begins the throw away lines.(.|\n)*', "", data)

with open("file.txt", "w") as f:
    f.writelines(subbed_data)

Output:

This is important line one.
This is important line two.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Regex remove all lines after specific line notepad++

Delete all lines after a specific line in PowerShell

Deleting all lines before a specific line python

Remove all lines after /

How to remove all empty lines after a specific match

Python: split into lines and remove specific line based on search

How to remove specific empty lines from multi line strings in python?

Remove all characters on line after a specific character in multiline textarea

Remove all lines before specific string starts python

A grep (or sed, or awk) command to select all lines that follow a specific pattern after a specific start line in a file

Remove lines by line number in python

Remove lines with specific line number specified in a file

sed remove end of line for specific lines

How to remove lines with specific pattern and next line

Remove all items after a specific item in a nested list python

How to remove all characters after a specific character in python?

iterate rows of a column and remove all text after specific words in python

Remove all after specific characters in a dataframe Using Python

How to remove all the lines of text before a specific text and all those after another specific text in multiple files using notepad++ or TextCrawler?

Remove all lines containing a specific string

Using sed to remove all lines after last time specific string is mentioned in TXT

How can I count a word from all lines that are 2 rows after a specific line?

inserting lines after specific line with loop

Remove all lines from text file up until specific string - python3

Remove a line if all fields match a specific pattern

How to skip n number of lines after finding a specific line in Python 3?

How to remove the rest of the line after a specific string

Remove line breaks between certain lines in python

Remove specific lines in json file using python