Django unicode转换

2024-09-29 01:23:43 发布

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

我对Unicode字符串做了一些研究,但不幸的是,我无法理解Python为什么会做一些事情

我有一段代码:

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实用程序不会读取它


Tags: store字符串代码nameoutputtypeunicodeproperties
1条回答
网友
1楼 · 发布于 2024-09-29 01:23:43

您应该使用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 "}}}

相关问题 更多 >