html - Python: what is the simplest way to convert utf8 string to � -


i have utf8 string non-ascii chars. need put html file in ampersand-hash-digits-semicolon form. best way this?

use .encode method 'xmlcharrefreplace' passed errors parameter:

in [1]: help(unicode.encode) on method_descriptor:  encode(...)     s.encode([encoding[,errors]]) -> string or unicode      encodes s using codec registered encoding. encoding defaults     default encoding. errors may given set different error     handling scheme. default 'strict' meaning encoding errors raise     unicodeencodeerror. other possible values 'ignore', 'replace' ,     'xmlcharrefreplace' other name registered     codecs.register_error can handle unicodeencodeerrors. 

in [2]: ustr = u'\xa9 \u20ac'  in [3]: print ustr © €  in [4]: print ustr.encode('ascii', 'xmlcharrefreplace') © € 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -