Django unicode convertion
问题描述:
我做了一些关于Unicode字符串的研究,但是我很遗憾无法弄清楚为什么Python会做一些事情。Django unicode convertion
我有这样一段代码:
output["anything"] = {
"type": "Feature",
"properties": {
"name": "somename",
"amenity": "Store",
"popupContent": "Store 3 "
},
}
当我使用print(output)
它打印此为:
{u'anything': u'type': u'Feature', u'properties': {u'amenity': u'Store', u'name': u'somename', u'popupContent': u'Store 3'}}
我却想有这个没有u' '
作为我的JavaScript工具赢得” t读这个。
如果答案也会给我一些洞察,为什么会发生这种情况,我真的很感激。
答
您应该使用json.dumps
而不是打印。
import json
output = {}
output["anything"] = {
"type": "Feature",
"properties": {
"name": "somename",
"amenity": "Store",
"popupContent": "Store 3 "
},
}
print(json.dumps(output))
输出:
{"anything": {"type": "Feature", "properties": {"name": "somename", "amenity": "Store", "popupContent": "Store 3 "}}}
“我不过喜欢这个没有'U”“'作为我的JavaScript实用程序将无法阅读。” - 如果您要转储JSON,请使用JSON自卸车,而不是“打印”。 'print'不打算与JSON兼容。 – user2357112
如果你真的有这段代码,'output'不会以这种方式打印,因为这些字符串都不是Unicode。你真的有什么?制作[mcve]。然而,'json'模块你需要什么。 –