Python: what does .encode('ascii', 'ignore') do?
python, unicode
Solution
encode(...)
S.encode([encoding[,errors]]) -> object
Encodes S using the codec registered for encoding. encoding defaults
to the default encoding. errors may be given to set a different error
handling scheme. Default is 'strict' meaning that encoding errors raise
a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
'xmlcharrefreplace' as well as any other name registered with
codecs.register_error that is able to handle UnicodeEncodeErrors.
So it encodes a unicode string to ascii and ignores errors
>>> "hello\xffworld".encode("ascii")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\xff' in position 5: ordinal not in range(128)
vs
>>> "hello\xffworld".encode("ascii", "ignore")
b'helloworld'
Problem
I am new to Python and am going over some code from work. I noticed there are a lot of lines that contain `row[0].encode('ascii', 'ignore')`. I did some reading and it seems like it is converting from unicode to bytes. Is it just a way to convert a string from u'string' to just string?