Brandon Macer
Brandon Macer

Reputation: 553

Adding a new string method in Python

I am parsing a bunch of HTML and am encountering a lot of "\n" and "\t" inside the code. So I am using

"something\t\n here".replace("\t","").replace("\n","")

This works, but I'm using it often. Is there a way to define a string function, along the lines of replace itself (or find, index, format, etc.) that will pretty my code a little, something like

"something\t\n here".noTabsOrNewlines()

I tried

class str:
    def noTabNewline(self):
        self.replace("\t","").replace("\n","")

but that was no good. Thanks for any help.

Upvotes: 1

Views: 136

Answers (2)

John Karasinski
John Karasinski

Reputation: 1006

While you could do something along these lines (https://stackoverflow.com/a/4698550/1867876), the more Pythonic thing to do would be:

myString = "something\t\n here" ' '.join(myString.split())

You can see this thread for more information: Strip spaces/tabs/newlines - python

Upvotes: 1

Nidhi Rai
Nidhi Rai

Reputation: 46

you can try encoding='utf-8'. otherwise in my opinion there is no other way otherthan replacing it . python also replaces it spaces with '/xa0' so in anyway you have to replace it. our you can read it line by line via (readline()) instead of just read() it .

Upvotes: 1

Related Questions