为什么我在python集中为每个元素的名字得到一个u字母?

时间:2014-01-17 19:10:48

标签: python twitter set

我正在使用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字母作为每个元素的前缀,为什么?

2 个答案:

答案 0 :(得分:1)

u表示该字符串是Unicode格式。

答案 1 :(得分:1)

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