String Cleaner Function



In [41]:
def removespecchar(test):
    import re
    if type(test) == str:
        test=re.sub('\t',' ',test)
        test=re.sub('\"',' ',test)
        test=re.sub('\n',' ',test)
        test=re.sub('\r',' ',test)
        test=re.sub(' +',' ',test)
    return(test)

In [42]:
def createstrings():
    strings=["Testing with \tcode     that can \"clean up \nstrings.",
             "Testing with \tcode that can  ''   \"clean up \nstrings.",
            "Testing with \tcode that can  \r\n   \"clean up \nstrings.",
            "Testing with \tcode that can   \r  \"clean up \nstrings.",
            "Testing with \tcode that can     \"clean up \nstrings."
            ]
    for string in strings:
        print(removespecchar(string))

In [43]:
createstrings()


Testing with code that can clean up strings.
Testing with code that can '' clean up strings.
Testing with code that can clean up strings.
Testing with code that can clean up strings.
Testing with code that can clean up strings.

In [44]:
import tkinter as tk

In [56]:
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self,master)
        self.pack()
        self.createWidgets()
        
    def createWidgets(self):
        self.clean=tk.Button(self, text="Clean Input",fg="green",command=(self.runStringCleaner))
        
        self.inputText=tk.Text(self,height=20,width=100)
        self.clean.pack()
        self.inputText.pack()
    def runStringCleaner(self):
        String=removespecchar(self.inputText.get(1.0,tk.END))
        self.inputText.delete(1.0,tk.END)
        self.inputText.insert(tk.END,String)

In [ ]:
root=tk.Tk()
app=Application(master=root)
app.mainloop()

In [ ]: