Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:将utf8字符串转换为&;的最简单方法是什么#0000;_Python_Html_Utf 8_String Conversion - Fatal编程技术网

Python:将utf8字符串转换为&;的最简单方法是什么#0000;

Python:将utf8字符串转换为&;的最简单方法是什么#0000;,python,html,utf-8,string-conversion,Python,Html,Utf 8,String Conversion,我有一个带有非ASCII字符的utf8字符串。我需要把它放在html文件中的符号和散列数字分号形式。最好的方法是什么 使用.encode方法,并将'xmlcharrefreplace'作为错误参数传递: In [1]: help(unicode.encode) Help on method_descriptor: encode(...) S.encode([encoding[,errors]]) -> string or unicode Encodes S using

我有一个带有非ASCII字符的utf8字符串。我需要把它放在html文件中的符号和散列数字分号形式。最好的方法是什么

使用
.encode
方法,并将
'xmlcharrefreplace'
作为
错误
参数传递:

In [1]: help(unicode.encode)
Help on method_descriptor:

encode(...)
    S.encode([encoding[,errors]]) -> string or unicode

    Encodes S using the codec registered for encoding. encoding defaults
    to the default encoding. errors may be given to set a different error
    handling scheme. Default is 'strict' meaning that encoding errors raise
    a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
    'xmlcharrefreplace' as well as any other name registered with
    codecs.register_error that can handle UnicodeEncodeErrors.


转换的是什么,UTF-8字节还是它们编码的Unicode码点?@gipi-是。增加了一个例子。
In [2]: ustr = u'\xa9 \u20ac'

In [3]: print ustr
© €

In [4]: print ustr.encode('ascii', 'xmlcharrefreplace')
© €