1

I am using Django to generate the abc.tex file I am displaying the the data in browser and same data i am writing to tex file like this

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)

Now in the broser i can see the text as speaker-hearer's but in the file it is coming as speaker-hearer's

How can i fix that

4

1 回答 1

1

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

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

对于您的示例字符串:

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

因此,只需在将输出写入文件时取消转义输出,并可能处理解析异常。

来源(见步骤#3)

于 2013-04-28T13:37:03.717 回答