1

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.

Brandon Macer
  • 473
  • 5
  • 8

2 Answers2

1

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

Community
  • 1
  • 1
John Karasinski
  • 977
  • 7
  • 16
  • I'll take this because it's pretty and that's what I wanted, though my curiosity remains for adding methods to "something" (or [somethings] or (somethings), for that matter) – Brandon Macer May 26 '16 at 03:49
  • You can see this previous post at http://stackoverflow.com/a/4698550/1867876. I updated my answer to include this above. – John Karasinski May 26 '16 at 03:52
1

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 .

Nidhi Rai
  • 36
  • 7