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. Show
Here is the code I have so far. I keep getting an error. I have also copied the error below.
Here is the error.
asked Jun 2, 2014 at 13:47
One way is to use re.sub, that's my preferred way.
Output:
Another way is to use re.escape:
Output:
Just a small tip about parameters style in python by
PEP-8 parameters should be Also if you want to keep the spaces just change answered Jun 2, 2014 at 14:01
Kobi KKobi K 7,4496 gold badges39 silver badges83 bronze badges 3 str.replace is the wrong function for what you want to do (apart from it being used incorrectly). You want to replace any character of a set with a space, not the whole set with a single space (the latter is what replace does). You can use translate like this:
This creates a mapping which maps every character in your list of special characters to a space, then calls translate() on the string, replacing every single character in the set of special characters with a space. answered Jun 2, 2014 at 14:02
rassahahrassahah 7265 silver badges6 bronze badges 4 You need to call
But this will not work, as replace looks for a substring, you will most likely need to use regular expression module
Don't forget the answered Jun 2, 2014 at 13:58
Danny MDanny M 4072 silver badges6 bronze badges replace operates on a specific string, so you need to call it like this
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. answered Jun 2, 2014 at 13:51
PavelPavel 7,2162 gold badges28 silver badges41 bronze badges 2 You can replace the special characters with the desired characters as follows,
answered Feb 12, 2015 at 16:08
surendransurendran 4801 gold badge8 silver badges18 bronze badges Translate seems faster: N=100000, 30 special characters, string length=70 replace: 0.3251810073852539 re.sub: 0.2859320640563965 translate: 0.12320685386657715 answered Sep 2 at 17:35
YanoYano 5985 silver badges8 bronze badges Not the answer you're looking for? Browse other questions tagged python string list replace urllib or ask your own question.How do I remove special characters from a number in Python?Remove Special Characters From the String in Python Using the str. isalnum() Method. The str. isalnum() method returns True if the characters are alphanumeric characters, meaning no special characters in the string.
How do you replace special characters?To insert special characters directly into the Find and Replace dialog box:. Select the Home tab in the ribbon.. Select Replace in the Editing group.. Ensure that the special character appears in the Find what text box in the Find and Replace dialog box. ... . Enter the replacement character in the Replace with text box.. How do you replace multiple special characters in Python?Use str. replace() to Replace Multiple Characters in Python.. Use re. sub() or re. subn() to Replace Multiple Characters in Python.. translate() and maketrans() to Replace Multiple Characters in Python.. Related Article - Python String.. How do I remove special characters from a string without removing space in Python?How to remove special characters from string Python (4 Ways). Method 1 – Using isalmun() method.. Method 2 – Using replace() method. Method 3 – Using filter(). Method 4 – Using join + generator function.. |