Python Forum
Delete lines from a string that contains a keyword
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Delete lines from a string that contains a keyword
#1
As stated in the title, I want to delete lines from a string that contains a certain keyword.
This is the first line
This is the second line
This is the third line
The keyword is second, so the output will be this.
This is the first line
This is the third line
How do I do this?
Reply
#2
Is it truly all in a single string? Generally want to split it by lines and then process the lines.

>>> print(s)
This is the first line
This is the second line
This is the third line

>>> print("\n".join(x for x in s.splitlines() if "second" not in x))
This is the first line
This is the third line
Reply
#3
(Oct-30-2020, 07:31 AM)bowlofred Wrote: Is it truly all in a single string? Generally want to split it by lines and then process the lines.

>>> print(s)
This is the first line
This is the second line
This is the third line

>>> print("\n".join(x for x in s.splitlines() if "second" not in x))
This is the first line
This is the third line

Yes, it is all in one string. Your code seems to work for me, thank you
Reply
#4
(Oct-30-2020, 05:39 AM)JellyCreeper6 Wrote: I want to delete lines from a string that contains a certain keyword.
How do I do this?

You can't do that. Strings are immutable so you cant delete lines. This does not mean that you can't achieve desired result but it will not be 'deleting lines from string'. It will be either printing selected parts from string or creating new string. In code above s have not changed and no lines are deleted from string.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
(Oct-30-2020, 10:24 AM)perfringo Wrote:
(Oct-30-2020, 05:39 AM)JellyCreeper6 Wrote: I want to delete lines from a string that contains a certain keyword.
How do I do this?

You can't do that. Strings are immutable so you cant delete lines. This does not mean that you can't achieve desired result but it will not be 'deleting lines from string'. It will be either printing selected parts from string or creating new string. In code above s have not changed and no lines are deleted from string.

Well, creating a new string is what I meant then, my bad Big Grin
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a specific keyword after another keyword and change the output sgtmcc 5 748 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Delete multiple lines from txt file Lky 6 2,207 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  Editing text between two string from different lines Paqqno 1 1,287 Apr-06-2022, 10:34 PM
Last Post: BashBedlam
  I want to simplify this python code into fewer lines, it's about string mandaxyz 5 2,047 Jan-15-2022, 01:28 PM
Last Post: mandaxyz
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,147 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  reading lines from a string [Solved] ebolisa 14 6,277 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Iterate 2 large text files across lines and replace lines in second file medatib531 13 5,707 Aug-10-2020, 11:01 PM
Last Post: medatib531
  Help to find a string and read the next lines crlamaral 4 2,408 Mar-19-2020, 09:24 AM
Last Post: Larz60+
  Can't seem to figure out how to delete several lines from a text file Cosmosso 9 4,033 Dec-10-2019, 11:09 PM
Last Post: Cosmosso
  Delete minimum occurence in a string RavCOder 10 3,851 Nov-12-2019, 01:08 PM
Last Post: RavCOder

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020