如何将表情符号unicode转换为表情符号?

2024-06-28 20:19:25 发布

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

我有一个.json文件,其中有超过70000条tweet,每条tweet都包含emojis。但是,我不确定如何将Unicode转换为实际的表情符号,以便用于情绪分析

这是我的.json文件中5条推文的示例:

{"text":"The morning is going so fast Part 2 of #DiscoveryDay is in full swing \ud83d\ude01\n\nGreat Atmosphere in the room \n\n#BIGSocial\u2026 https:\/\/t.co\/P08qBoH6tv"}
{"text":"Double kill! #XiuKai lives! I died. \ud83d\ude0c https:\/\/t.co\/QCyk3r2JCb"}
{"text":"ALLTY \ud83d\udc94"}
{"text":"Shouldn\u2019t be normal for a 24 year old to be this tiered \ud83d\udca4"}
{"text":"@TheNames_BrieX Trust me! \ud83d\udcaf"}

现在,我如何将所有tweet的unicode转换为实际的表情符号?例如,如何将\ud83d\ude0c转换为实际的表情符号

可以使用哪些方法将unicode转换为实际的表情符号


Tags: 文件textinhttpsjsonisunicodebe
3条回答

\ud83d\udcaf这样的字符串是由不正确的处理引起的,可以由data['text'].encode('utf-16', 'surrogatepass').decode('utf-16')修复reference

如果您尝试按规则进行情绪分析,上面的代码可以在您的终端中显示实际的表情图标,并且您可以为其构建标签映射,无需转换原始文本

如果您尝试基于统计或深度学习模型的情感分析,他们可以通过统计特征或监督学习捕获语义信息,并且这些表情符号可以自动识别为重要特征

如果这是您的实际JSON文件内容:

{"text":"The morning is going so fast Part 2 of #DiscoveryDay is in full swing \ud83d\ude01\n\nGreat Atmosphere in the room \n\n#BIGSocial\u2026 https:\/\/xxx\/P08qBoH6tv"}
{"text":"Double kill! #XiuKai lives! I died. \ud83d\ude0c https:\/\/xxx\/QCyk3r2JCb"}
{"text":"ALLTY \ud83d\udc94"}
{"text":"Shouldn\u2019t be normal for a 24 year old to be this tiered \ud83d\udca4"}
{"text":"@TheNames_BrieX Trust me! \ud83d\udcaf"}

然后就是JSON Lines格式,其中每一行都是一个完整的JSON结构,而不是一个有效的JSON文件

每次读一行,就像这样:

import json
with open('test.json') as f:
    for line in f:
        print(json.loads(line))

输出:

{'text': 'The morning is going so fast Part 2 of #DiscoveryDay is in full swing 😁\n\nGreat Atmosphere in the room \n\n#BIGSocial… https://xxx/P08qBoH6tv'}
{'text': 'Double kill! #XiuKai lives! I died. 😌 https://xxx/QCyk3r2JCb'}
{'text': 'ALLTY 💔'}
{'text': 'Shouldn’t be normal for a 24 year old to be this tiered 💤'}
{'text': '@TheNames_BrieX Trust me! 💯'}

请注意,我不得不改变原来的小网址,因为这样不允许内容与他们

如您所说,如果这只是JSON行的一个示例,并且它是一个格式完整、正确的JSON文件,那么只需使用json.load读取它即可:

import json
with open('test.json') as f:
    print(json.load(f))

表情符号是unicode的一个子集。因此,从unicode到表情符号的转换既没有必要也没有可能。只需将数组更改为

var data = ["\u{1F642}", "\u{1F603}"]

如果输入的是十六进制数,则可以使用

String.fromCodePoint(parseInt ("1F929", 16))

在HTML中,还可以使用HTML十六进制实体

"&#x" + "1F618" + ";"

相关问题 更多 >