当使用pyqt qtextedit时,如何从文本中删除一组任意大小的字符?

2024-09-28 22:01:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要从QTextEdit块中删除任意大小的字符集,并在替换字符中没有空格的地方用零替换它。你知道吗

我知道cursor.insertText()cursor.deleteChar()的方法 但是我需要一种不依赖于游标和操作系统的方法来处理QTextEdit块中的任意字符集


Tags: 方法地方字符cursor空格游标字符集qtextedit
1条回答
网友
1楼 · 发布于 2024-09-28 22:01:17

通过QTextEdit.toHtml()toPlainText()获得字符串。你知道吗

因为你得到了一组简单的字符串,所以你只需要这样做。你知道吗

plaintext = QTextEdit.toPlainText()
plaintext = plaintext.replace("the size of string","0"*len("the size of string")
QTextEdit.setPlainText(plaintext)

在toHtml的例子中,您得到标签中的文本部分,并用它替换您喜欢的字符串。你知道吗

htmltext = QTextEdit.toHtml()
htmltext = htmltext.replace("<p>your favorite strings<\p>","<p"+"0"*len(your favorite strings)+"<\p>")
QTextEdit.setHtml(htmltext)

您可能需要提前通过lxml模块等分析html。 这对你来说是个好建议吗?你知道吗

相关问题 更多 >