import codecs # Any custom handler that returns an empty string for some inputs works codecs.register_error('test.ignore', codecs.ignore_errors) # Any string containing enough (43 in my case) characters that can't be encoded in the target encoding works # '\udc00' works for all the encodings in question (eg. '\x90' works for all except 'gb18030') data = 100 * '\udc00' # Any encoding that uses _multibytecodec works data.encode('cp950', 'test.ignore')