【问题标题】:Tweepy: Always display emoji in "\ud83d\ude4c" format from tweet textTweepy:始终以“\ud83d\ude4c”格式从推文文本中显示表情符号
【发布时间】:2016-09-12 15:35:24
【问题描述】:

我的问题

当使用 tweepy 流式传输数据时,我收到了

的预期结果
Tweet Contents: RT @ChickSoPretty: Zendaya tho \ud83d\ude4c https:....

使用代码时

def on_data(self, data):
    username = data.split(',"screen_name":"')[1].split('","location"')[0]
    tweet = data.split(',"text":"')[1].split('","source')[0]
    print("Tweet Contents: " + tweet)

--- 我目前正在跟踪 u'\U0001f64c',一个表情符号的代码。 ---

但是,当我尝试输出其余用户最近的推文时...

for status in tweepy.Cursor(api.user_timeline, id=username).items(20):
    tweet = status.text
    print("Tweet Contents: " + tweet)

如果“用户名”是最近使用过表情符号的用户,我的程序会崩溃。

这是可以理解的,因为我现在正尝试在控制台上打印一个表情符号,而不是我最初所做的,而是显示 Javascript 转义代码 \ud83d\ude4c。。 p>

我的问题是,我如何阅读用户的状态并以第一种格式输出他们的推文?

我的代码的目的

我的长期目标是遍历用户的状态,并检查他们在最近的 20 条推文中使用了多少表情符号(包括 RT 和回复)。

当表情符号以 Javascript/Java Escape 格式显示时,我已经“成功创建”了一些用于检测推文中表情符号的杂乱代码,如下...

for character in tweet:
  iteration = iteration + 1
  if(iteration < tweetLength):
    if tweet[iteration] == '\\' and tweet[iteration + 1] == 'u' and tweet[iteration + 6] == '\\' and tweet[iteration + 7] == 'u':           
    for x in range(0,12):
      emojiCode += tweet[iteration + x]                                        
      numberOfEmojis = numberOfEmojis + 1
      print("Emoji Code Found: "+emojiCode)  
      emojiCode = ""          
      iteration = iteration + 7

哇,真是一团糟。但是,它适用于我需要它做的事情(仅限英文推文)。

有没有更好的方法?我应该废弃这个并使用

tweet.encode('utf-8')

并尝试查找以下输出格式的表情符号?

b'@Jathey3 @zachnahra31 this hard\xf0\x9f\x98\x82 we gotta do this https:...'

我正在使用 Python 3.4.2

【问题讨论】:

    标签: python twitter unicode tweepy emoji


    【解决方案1】:

    有没有更好的办法?

    是的:不要尝试使用低级的逐个字符的字符串摆弄来处理 JSON 格式的数据。标准库中有一些工具可以更快、更可靠地完成此任务。

    搜索 JSON 字符串文字编码形式的字符很棘手,因为您不知道它是包含为 \ud83d\ude4c 还是简单地包含原始字符 ?(U+1F64C 个人提升双手庆祝)。并且任何其他非表情符号字符也可能被编码为\u 转义,例如\u0061\u0061aa。还有一些关于当你有双反斜杠或转义引号时会发生什么的规则,这在查找字符的同时很难处理,并且当你使用时,属性顺序和空格格式会出现很多问题试图找到你想要的属性。

    使用json 模块的loads 方法将JSON 字符串解码为包含可以直接检查的原始字符串的Python 字典,从而避免所有这些陷阱。

    然后要查找一定范围内的字符,有正则表达式,由re模块提供。

    最后,如果您想以 JSON 格式将输出显示为 \ud83d\ude4c,您可以使用 json.dumps 方法将该输出编码回 JSON。

    # Assuming input like:
    json_input= '{"screen_name":"fred","location":"home","text":"Here is an emoji: ?... and here is another one ?"}'
    
    import json, re
    emoji_pattern = re.compile('[\U0001F300-\U0001F64F]')
    
    dict_input = json.loads(json_input)
    text = dict_input['text']
    screen_name = dict_input['screen_name']
    emojis = emoji_pattern.findall(text)
    
    print(len(emojis), 'chars found in post by', screen_name)
    for emoji in emojis:
        print('emoji: ' + json.dumps(emoji))
    
    2 chars found in post by fred
    Character: "\ud83d\ude4c"
    Character: "\ud83d\udca9"
    

    (这假设只有 U+1F300 到 U+1F64F 范围内的字符才算作真正的表情符号。还有其他字符可以被归类为表情符号,但这是另一种蠕虫。另外,未来的 Unicode 版本可能会添加更多新角色。)

    (旁注:re 中的 \U 不适用于 Python 3.3 之前“窄”Python 构建的用户。)

    【讨论】:

    • 很好的答案!对于其他使用 tweepy 用于相同目的的人,您可以使用 dict_input = status._json 跳过 json.loads 命令,其中 status 是获取的时间线项目,如 tweepy 文档中所示 here
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2019-02-16
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多