我想删除<>中的所有文本。我使用了replace('',''),但它根本没有删除它们。
例如,我的文字是check out here我希望它变成“ Nitin Hardeniya-2015-计算机”。
请告诉我如何纠正我的代码。
这里是代码:
import tkinter as tk
root = tk.Tk()
text = root.clipboard_get()
def onclick():
text_widget.delete("1.0", "end")
text_widget.insert(tk.END, text.replace('<*>',' '))
root = tk.Tk()
text_widget = tk.Text(root)
text_widget.insert(tk.END, text) #inserting the data in the text widget
text_widget.pack()
button = tk.Button(root, text = "Press me!", command = onclick)
button.pack()
root.mainloop()
您需要使用正则表达式进行此类匹配。您行中的<*>
:text.replace('<*>',' ')
不是正则表达式,它是严格匹配的内容(请参见replace
函数文档)