# -*- coding: utf-8 -*- import codecs def w3lib_replace(exc): return (u'\ufffd', exc.start+1) codecs.register_error('w3lib_replace', w3lib_replace) data_str = open("test_string.bin", "rb").read() data_str.decode("utf-16-le", 'w3lib_replace')