def force_encoding(text, encoding):
 return text.encode(encoding, errors='replace').decode(encoding)

def force_encoding ( text , encoding ) : return text . encode ( encoding , errors = 'replace' ) . decode ( encoding )
make sure a given text is encodable with the given encoding .