Search code examples
pythonpython-3.xqplaintextedit

How to delete a textline with matched text in QPlainTextEdit?


Below lines are in A QPlainTextEdit:

enter image description here

I want to delete a matched line with line variable help. For example, I want to delete

line 2 s44 grade

with the help of line variable(line = "line 2")

I am able to delete the particular text with below code:

item = "line 2"
text = self.project_length_lanes_plainedit.toPlainText()
text = text.replace(item, '')
_list = text.split()
text = '\n'.join(_list)
self.project_length_lanes_plainedit.setPlainText(text)

but I want to delete the entire line. How can I do that?


Solution

  • I tried as below working:

        item = "line 2"
        text = self.project_length_lanes_plainedit.toPlainText()
        for l in text.split('\n'):
            if item in l:
                text = text.replace(l, '')
                _list = text.split('\n')
                _list = [ i for i in _list if i ]
                text = '\n'.join(_list)
                self.project_length_lanes_plainedit.setPlainText(text)