Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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 json将unicode转换为字符串,这样我就可以在django html页面中使用它了_Python_Json_Django_Unicode - Fatal编程技术网

Python json将unicode转换为字符串,这样我就可以在django html页面中使用它了

Python json将unicode转换为字符串,这样我就可以在django html页面中使用它了,python,json,django,unicode,Python,Json,Django,Unicode,编辑: 任何人都可以告诉我如何解码unicode。我只想将json unicode打印到我开发的html页面中。我从你那里得到了api 几乎每一步我都走对了。但输出是unicode,我不知道如何提取内容并显示到我的页面中 我需要打印内容。怎么做 我的观点 template_vars['kural'] = json.dumps(thirukural[x]) t = loader.get_template('index.html') c = Context(templ

编辑:

任何人都可以告诉我如何解码unicode。我只想将json unicode打印到我开发的html页面中。我从你那里得到了api

几乎每一步我都走对了。但输出是unicode,我不知道如何提取内容并显示到我的页面中

我需要打印内容。怎么做

我的观点

template_vars['kural'] = json.dumps(thirukural[x])
        t = loader.get_template('index.html')
        c = Context(template_vars)
        #pprint.pprint(c)

        return HttpResponse(t.render(c))
Html页面

  <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head><body>

<p id="p"></p>

<script type="text/javascript">
var t = {{kural|safe}}
var text = eval(t);
var p = document.getElementById("p");
p.innerHTML=t.kural;
</script>
</body></html>
</body>
</html>
但是在heroku api页面中,示例输出是这样打印的

யாதனின் யாதனின் நீங்கியான் நோதல் அதனின் அதனின் இலன்.
  {
  "id": "213",
  "kural": "புத்தே ளுலகத்தும் ஈண்டும் பெறலரிதே\n\nஒப்புரவின் நல்ல பிற."
  }

您可以看到,我的输出没有这样的换行符\n。我该怎么做呢?

我相信您要做的是将视图代码显示的第一行更改为:

模板_vars['kural']=thirukural[x]。编码'ascii','xmlcharrefreplace'

这会将所有内容都更改为HTML实体,最终会显示如下内容:

&2953&2994;&2965;&2990;&3021; &2980;&2996;&3008;&2951;&2991; &2980;&3018;&2975;&3021;&2986;&2990;&3021; &2990;&2994;&2992;&3021;&2980;&2994;&3009;&2990;&3021;\n\n&2965&3010;&2990;&3021;&2986;&2994;&3009;&2990;&3021; &2951;&2994;&3021;&2994; &2980;&2993;&3007;&2997;&3009;.' kural变量是一个dict,如果您想在视图中显示kural,我想您需要json

import json
template_vars['kural'] = json.dumps(thirukural[x])

你试过{{kural | safe}}吗?thirukural[x]包含相同的内容,它以HTML页面打印。我试过{{kural | safe}也只是相同的结果…现在错误是'dict'对象没有属性'encode',实际上它只替换字符,但我需要从unicode返回文本。这些字符是HTML实体,当您在模板中输出它们时,它们应该在web浏览器中正确呈现。不过你可能得通过保险箱过滤。好吧,我放弃了。我试过保险箱,但回来时我累了。无论如何,现在输出预期的结果,但换行符不能正常工作。你能给我指点路吗?我试过你的建议了。它删除了u',html没有任何改变,所以我的朋友想出了一个主意。我编辑了我的问题,请查看并帮助我。我是如何在stackoverflow中提出这个问题的