gpt4 book ai didi

python - 如何在 mako 中正确转义输出(对于 XHTML)?

转载 作者:行者123 更新时间:2023-11-28 16:54:38 29 4
gpt4 key购买 nike

尽管提供了一种使用过滤器转义输出的好方法,但它们都做不到正确的事情。获取字符串:

x=u"&\u0092"

过滤器执行以下操作:

x             Turns the & into an entity but not the \u0092 (valid XML but not XHTML)
h Exactly the same
u Escapes both, but obviously uses url escaping
entities Only converts named entities, so again only the & is escaped
decode.latin1 The same

HTML uses the standard UNICODE Consortium character repertoire, and it leaves undefined (among others) 65 character codes (0 to 31 inclusive and 127 to 159 inclusive)

这些似乎是遗漏的字符。有什么想法吗?

编辑

如果我离线使用该文件,它似乎可以验证。这可能是内容类型问题吗?

最佳答案

没有必要将 Unicode 字符转换为 &#xxxx;除非您有意使用 ASCII 字符集,否则表单无法在 HTML 中工作。转义命名实体,然后将整个字符串编码为 UTF-8 并像这样写出来更简单、更有效。您可能应该声明在 HTTP header 或 <meta> 中使用的编码标签。

编辑:

It seems to validate if I use the file offline. Could this be a Content-Type problem?

是的。您可以使用 HTTP header 强制使用 UTF-8 字符集,也可以直接通过元标记在 HTML 中指定它:

<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8" />

关于python - 如何在 mako 中正确转义输出(对于 XHTML)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2125788/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com