Your string is already encoded with some encoding. Before encoding it to ascii, you must decode it first.
Python is implicity trying to decode it (That's why you get a UnicodeDecodeError not UnicodeEncodeError).
You can solve the problem by explicity decoding your bytestring (using the appropriate encoding) before trying to reencode it to ascii.
Example:
s = s.decode('some_encoding').encode('ascii', 'replace')
Use the correct encoding your string was encoded in first place, instead of 'some_encoding'.
You have to know which encoding a string is using before you can decode it. Where did you get the string from?
Answer from nosklo on Stack OverflowYour string is already encoded with some encoding. Before encoding it to ascii, you must decode it first.
Python is implicity trying to decode it (That's why you get a UnicodeDecodeError not UnicodeEncodeError).
You can solve the problem by explicity decoding your bytestring (using the appropriate encoding) before trying to reencode it to ascii.
Example:
s = s.decode('some_encoding').encode('ascii', 'replace')
Use the correct encoding your string was encoded in first place, instead of 'some_encoding'.
You have to know which encoding a string is using before you can decode it. Where did you get the string from?
encode should be used on unicode objects to convert it to a str.
If you have a str object, you should use decode to convert it to a unicode.