Decode the string to Unicode. Assuming it's UTF-8-encoded:
str.decode("utf-8")Call the
replacemethod and be sure to pass it a Unicode string as its first argument:str.decode("utf-8").replace(u"\u2022", "*")Encode back to UTF-8, if needed:
str.decode("utf-8").replace(u"\u2022", "*").encode("utf-8")
(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 str shadows the built-in type str.)
Decode the string to Unicode. Assuming it's UTF-8-encoded:
str.decode("utf-8")Call the
replacemethod and be sure to pass it a Unicode string as its first argument:str.decode("utf-8").replace(u"\u2022", "*")Encode back to UTF-8, if needed:
str.decode("utf-8").replace(u"\u2022", "*").encode("utf-8")
(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 str shadows the built-in type str.)
Encode string as unicode.
>>> special = u"\u2022"
>>> abc = u'ABC•def'
>>> abc.replace(special,'X')
u'ABCXdef'
I have a csv file that has greek symbols which are stored in gibberish looking characters.
For example, the symbol delta is stored as ∆
I would like to replace ∆ and other weird characters like these in my string with the greek symbols Δ , α etc. How can I do this?
How do I remove non-ASCII characters in Python?
How do I remove only selected Unicode symbols?
Why should I avoid removing all Unicode?
Instead of scraping entirely via xpath like I have in the past, I decided to pull the data out of a clearly visible var in a <script> element and write a function to parse it as a dictionary recursively*.
My problem is that many characters are escaped, so instead of https:// I have https:\u002F\u002F, literally -- 12 characters instead of those 2 forward slashes, the \u**** sequence doesn't stand for the frontslashes. I have this problem in body text too, and because there are lots of diacritical marks I can't get away with replacing them bluntly. I need to replace these substrings with the corresponding characters, so \u002F -> /, \u00C9 -> é, etc.
It seems like there should be an obvious and not-ridiculous solution for this, but I don't know what it is and googling turns up different problems. Is this something I need to anticipate at the scraping stage? I'm using the requests and html modules if it matters.
*Which is dumb because it was already formatted just like a dict, so I could probably just have saved the raw text in a .py and imported it as a module
The string you're searching for to replace must also be a Unicode string. Try:
newToke = thisToken.replace(u'\u2013','')
You can see the answer in this post: How to replace unicode characters in string with something else python?
Decode the string to Unicode. Assuming it's UTF-8-encoded:
str.decode("utf-8")
Call the replace method and be sure to pass it a Unicode string as its first argument:
str.decode("utf-8").replace(u"\u2022", "")
Encode back to UTF-8, if needed:
str.decode("utf-8").replace(u"\u2022", "").encode("utf-8")
If you have a bytestring (undecoded data), use the 'replace' error handler. For example, if your data is (mostly) UTF-8 encoded, then you could use:
decoded_unicode = bytestring.decode('utf-8', 'replace')
and U+FFFD � REPLACEMENT CHARACTER characters will be inserted for any bytes that can't be decoded.
If you wanted to use a different replacement character, it is easy enough to replace these afterwards:
decoded_unicode = decoded_unicode.replace('\ufffd', '#')
Demo:
>>> bytestring = b'F\xc3\xb8\xc3\xb6\xbbB\xc3\xa5r'
>>> bytestring.decode('utf8')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'utf8' codec can't decode byte 0xbb in position 5: invalid start byte
>>> bytestring.decode('utf8', 'replace')
'Føö�Bår'
Thanks to you for your comments. This way I was able to implement a better solution:
try:
s2 = codecs.encode(s, "utf-8")
return (True, s, None)
except Exception as e:
ret = codecs.decode(codecs.encode(s, "utf-8", "replace"), "utf-8")
return (False, ret, e)
Please share any improvements on that solution. Thank you!
There seem to be two problems with your program.
Firstly, you are passing the wrong code point to chr(). The hexdecimal code point of the character ’ is 0x2019, but you are passing in the decimal number 2019 (which equates to 0x7e3 in hexadecimal). So you need to do either:
temp = temp.replace(chr(0x2019), "'") # hexadecimal
or:
temp = temp.replace(chr(8217), "'") # decimal
in order to replace the character correctly.
Secondly, the reason you are getting the error is because some other part of your program (probably the database backend) is trying to encode unicode strings using some encoding other than UTF-8. It's hard to be more precise about this, because you did not include the full traceback in your question. However, the reference to "charmap" suggests a Windows code page is being used (but not cp1252); or an iso encoding (but not iso8859-1, aka latin1); or possibly KOI8_R.
Anyway, the correct way to deal with this issue is to ensure all parts of your program (and especially the database) use UTF-8. If you do that, you won't have to mess about replacing characters anymore.
you can Encode your unicode string to convert to type str :
a=u"dataàçççñññ"
type(a)
a.encode('ascii','ignore')
this way it will delete the special characters will return you 'data'.
other way you can use unicodedata
I believe the problem is that Vim not opening the file using the right encoding. Your file is in encoding cp1252 but Vim doesn't detect it and guess utf-8.
I would propose to open the file with the ++enc=cp1252 flag.
:e ++enc=cp1252 filepath
To improve the way Vim guess the encoding you can add the following lines (Vim would first try to open as cp1252 and then as utf-8):
:set fileencodings=cp1252,utf-8
I would propose you:
:s/\([\x80-\xFF]\)/\='0x'.printf('%02x', char2nr(submatch(1)))/g