replace special characters in a string python

后端 未结 5 832
感动是毒
感动是毒 2020-12-04 20:06

I am using urllib to get a string of html from a website and need to put each word in the html document into a list.

Here is the code I have so far. I keep getting a

5条回答
  •  天命终不由人
    2020-12-04 20:28

    replace operates on a specific string, so you need to call it like this

    removeSpecialChars = z.replace("!@#$%^&*()[]{};:,./<>?\|`~-=_+", " ")
    

    but this is probably not what you need, since this will look for a single string containing all that characters in the same order. you can do it with a regexp, as Danny Michaud pointed out.

    as a side note, you might want to look for BeautifulSoup, which is a library for parsing messy HTML formatted text like what you usually get from scaping websites.

提交回复
热议问题