如何在Python中将unicode字符串格式化为utf-8?

发布于 2024-10-08 05:41:30 字数 1368 浏览 2 评论 0原文

我正在读取一个 JSON 字符串,其中散布着 u'string' 样式的字符串。示例:

 [
     {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1996"
        }
      }, 
      "guid": "#9202a8c04000641f80000000003a0ee6", 
      "type": "\/games\/game", 
      "id": "\/en\/el_grande", 
      "name": "El Grande"
    }, 
    {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1995"
        }
      }, 
      "guid": "#9202a8c04000641f80000000000495ec", 
      "type": "\/games\/game", 
      "id": "\/en\/settlers_of_catan", 
      "name": "Settlers of Catan"
    }
  ]

如果我指定 name = result.name。然后,当我记录该值传递给 Django 模板时,它显示为 u'Dominion'

如何将其格式化以显示为 Dominion?

++ UPDATE ++

我认为问题与从列表或字典中打印值有关。例如:

result = freebase.mqlread(query)

games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)

这显示为:

(u'Dominion',)  // saved response to dictionary, and then printed
Dominion        // when calling the value directly from the response

我需要迭代 JSON 项目数组,并且值用 unicode 显示。为什么?

I'm reading in a JSON string which is littered with u'string' style strings. Example:

 [
     {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1996"
        }
      }, 
      "guid": "#9202a8c04000641f80000000003a0ee6", 
      "type": "\/games\/game", 
      "id": "\/en\/el_grande", 
      "name": "El Grande"
    }, 
    {
      "!\/award\/award_honor\/honored_for": {
        "award": {
          "id": "\/en\/spiel_des_jahres"
        }, 
        "year": {
          "value": "1995"
        }
      }, 
      "guid": "#9202a8c04000641f80000000000495ec", 
      "type": "\/games\/game", 
      "id": "\/en\/settlers_of_catan", 
      "name": "Settlers of Catan"
    }
  ]

If I assign name = result.name. Then when I log of pass that value to a Django template, it displays as u'Dominion'

How do I format it to display as Dominion?

++ UPDATE ++

I think the problem has to do with printing values from a list or dictionary. For example:

result = freebase.mqlread(query)

games = {}
count = 0
r = result[0]
name = r.name
games["name"] = name,
self.response.out.write(games["name"])
self.response.out.write(name)

This displays as:

(u'Dominion',)  // saved response to dictionary, and then printed
Dominion        // when calling the value directly from the response

I need to iterate through an array of JSON items and the values are being shown with the unicode. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

入怼 2024-10-15 05:41:30

games["name"] = name, 末尾的逗号使其成为 1 元组。将其删除。

The comma at the end of games["name"] = name, makes it a 1-tuple. Remove it.

如何视而不见 2024-10-15 05:41:30
>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze

对 Django 不太了解,但不接受 snicode 字符串对我来说似乎不符合 Python 风格。

>>> # example
>>> s = u"Jägermütze"
>>> s.encode("utf-8")
'J\xc3\xa4germ\xc3\xbctze'
>>> print s.encode("utf-8") # on a utf-8 terminal
Jägermütze

Don't know much about Django, but not accepting snicode strings seems unpythonic to me.

甜扑 2024-10-15 05:41:30

您可以使用 str(your string) 来执行此操作。

You can use str(your string) to do this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文