I am having some trouble with a very basic string issue in Python (that I can't figure out). Basically, I am trying to do the following:
'# read file into a string myString = file.read() '# Attempt to remove non breaking spaces myString = myString.replace("\u00A0"," ") '# however, when I print my string to output to console, I get: Foo **<C2><A0>** Bar
I thought that the "\u00A0" was the escape code for unicode non breaking spaces, but apparently I am not doing this properly. Any ideas on what I am doing wrong?
strip() Python String strip() function will remove leading and trailing whitespaces. If you want to remove only leading or trailing spaces, use lstrip() or rstrip() function instead.
The replaceAll() method of the String class replaces each substring of this string that matches the given regular expression with the given replacement. You can remove white spaces from a string by replacing " " with "".
Using 'str.replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.
In python, to remove Unicode character from string python we need to encode the string by using str. encode() for removing the Unicode characters from the string.
You don't have a unicode string, but a UTF-8 list of bytes (which are what strings are in Python 2.x).
Try
myString = myString.replace("\xc2\xa0", " ")
Better would be to switch to unicode -- see this article for ideas. Thus you could say
uniString = unicode(myString, "UTF-8") uniString = uniString.replace(u"\u00A0", " ")
and it should also work (caveat: I don't have Python 2.x available right now), although you will need to translate it back to bytes (binary) when sending it to a file or printing it to a screen.
No, u"\u00A0"
is the escape code for non-breaking spaces. "\u00A0"
is 6 characters that are not any sort of escape code. Read this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With