I have a string that I got from reading a HTML webpage with bullets that have a symbol like “•” because of the bulleted list. Note that the text is an HTML source from a webpage using Python 2.7’s urllib2.read(webaddress).
I know the unicode character for the bullet character as U+2022, but how do I actually replace that unicode character with something else?
I tried doing
str.replace("•", "something")
but it does not appear to work… how do I do this?
Decode the string to Unicode. Assuming it’s UTF-8-encoded:
Call the
replacemethod and be sure to pass it a Unicode string as its first argument:Encode back to UTF-8, if needed:
(Fortunately, Python 3 puts a stop to this mess. Step 3 should really only be performed just prior to I/O. Also, mind you that calling a string
strshadows the built-in typestr.)