【问题标题】:Why am I getting a u letter for each element's name in a python set? [duplicate]为什么我在 python 集中为每个元素的名称得到一个 u 字母? [复制]
【发布时间】:2014-02-07 06:10:56
【问题描述】:

我正在使用 Twitter API 对 python 的 twitter 库进行测试,我请求了特定 WOEID 的所有热门话题,称为 mexican_trends

然后我尝试获取集合中的每个特定趋势名称,尝试这样:

trendsSet = set(trend['name']
            for trend in mexican_trends[0]['trends'])

但是print 转储一个u 字母作为trendSet 中每个元素的前缀。

这是print json.dumps(mexican_trends, indent=2)

[   {
    "created_at": "2014-01-17T18:51:20Z", 
    "trends": [
      {
        "url": "http://twitter.com/search?q=%23MentirasQueNoTienenPerdon", 
        "query": "%23MentirasQueNoTienenPerdon", 
        "name": "#MentirasQueNoTienenPerdon", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%23FelizCumpleJLRoma", 
        "query": "%23FelizCumpleJLRoma", 
        "name": "#FelizCumpleJLRoma", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%23EsDeChakas", 
        "query": "%23EsDeChakas", 
        "name": "#EsDeChakas", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%22Jos%C3%A9+Sulaim%C3%A1n%22", 
        "query": "%22Jos%C3%A9+Sulaim%C3%A1n%22", 
        "name": "Jos\u00e9 Sulaim\u00e1n", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%23RioRomaEnRitmoson", 
        "query": "%23RioRomaEnRitmoson", 
        "name": "#RioRomaEnRitmoson", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%23TemasPerfectos", 
        "query": "%23TemasPerfectos", 
        "name": "#TemasPerfectos", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=%22Juan+Gelman%22", 
        "query": "%22Juan+Gelman%22", 
        "name": "Juan Gelman", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=Michoac%C3%A1n", 
        "query": "Michoac%C3%A1n", 
        "name": "Michoac\u00e1n", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=M%C3%A9xico", 
        "query": "M%C3%A9xico", 
        "name": "M\u00e9xico", 
        "promoted_content": null, 
        "events": null
      }, 
      {
        "url": "http://twitter.com/search?q=TLCAN", 
        "query": "TLCAN", 
        "name": "TLCAN", 
        "promoted_content": null, 
        "events": null
      }
    ], 
    "as_of": "2014-01-17T18:59:14Z", 
    "locations": [
      {
        "woeid": 23424900, 
        "name": "Mexico"
      }
    ]   
  } 
]

trendsSet 打印为:

set([u'#RioRomaEnRitmoson', u'M\xe9xico', u'#MentirasQueNoTienenPerdon', u'TLCAN', u'#EsDeChakas', u'#FelizCumpleJLRoma', u'Juan Gelman', u'#TemasPerfectos', u'Jos\xe9 Sulaim\xe1n', u'Michoac\xe1n'])

如前所述,uith u 字母作为每个元素的前缀,为什么?

【问题讨论】:

  • 妈的,没错。我实际上在 python 数组中查找了 u,但乍一看没有得到结果......因为 mexican_trends 实际上是一个列表。
  • @Wooble:投票决定以 dup 的身份结束,而不仅仅是发表评论。这为 OP 提供了更多信息,防止在搜索未来用户时出现数百个重复,等等。
  • @abarnert:几小时前我的票数已经用完了。

标签: python twitter set


【解决方案1】:

字符串前面的 'u' 字符表示该字符串是一个 unicode 字符串。 http://docs.python.org/2/howto/unicode.html 的 python 文档和其他有关堆栈交换的问题(例如 Usage of unicode() and encode() functions in Python)可以帮助您,如果您有特定用途,或者需要使用它们。

【讨论】:

    【解决方案2】:

    u 表示字符串是 Unicode 格式。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2013-06-28
      • 1970-01-01
      • 2021-01-05
      • 1970-01-01
      相关资源
      最近更新 更多