Ascii以html编码

时间:2015-02-10 16:40:07

标签: python html encoding

如何将字符串(ascii)编码为html代码?
例如,string = "encoding html"
结果应该在编码string_encoded = "encoding html"

之后

1 个答案:

答案 0 :(得分:0)

我认为您只需要使用cgi.escape来替换字符<,>和&amp ;.对于大多数情况,这将是你所需要的。例如:

>>> import cgi
>>> cgi.escape("<Foo & Bar>") 
'&alt;Foo &amp; Bar&gt;'

除非你强行在标记中添加一个空格,否则&nbsp;符号并不是真正需要的,没有图书馆自然会为你做这些。