Python-Django在fi中以编码格式编写ascii字符

2024-05-23 14:53:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Django生成abc.tex文件 我显示的数据在浏览器和相同的数据,我写到tex文件这样

with open("sample.tex") as f:
    t = Template(f.read())

head = ['name','class']
c = Context({"head":headers, "table": rowlist})

# Render template
output = t.render(c)

with open("mytable.tex", 'w') as out_f:
    out_f.write(output)

现在在broser中我可以看到文本为speaker-hearer's,但在文件中它是speaker-hearer's

我该怎么修


Tags: 文件数据sampledjangooutputaswith浏览器
1条回答
网友
1楼 · 发布于 2024-05-23 14:53:16

据我所知,浏览器会自动解码这些数据,但文件中的文本将是原始的;因此您看到的是“原样”的数据。你知道吗

在写入abc.tex文件之前,也许可以使用HTMLParser库对Django(output)生成的数据进行解码。你知道吗

对于示例字符串:

import HTMLParser
h = HTMLParser.HTMLParser()
s = "speaker-hearer's"
s = h.unescape(s)

因此,当您将输出写入文件时,只需取消对输出的回避,并可能处理解析异常。你知道吗

Source(参见步骤#3)

相关问题 更多 >