如何将从txt文件读取的代理对转换回python 3中的表情符号?

问题描述

我有一些txt文件,可在其中包含字符串的地方读取,例如:

“是的!沙丁鱼罐头隔开!\ uD83E \ uDD23”

问题是:当我在做

"Yes! Sardines in a can distancing! \uD83E\uDD23".encode('utf-16','surrogatepass' ).decode('utf-16)

由于python分别将\ UDD23或\ UD83E视为两个单个字符,因此将unicode点转换为表情符号。

输出

Yes! Sardines in a can distancing! ? 

此外,当我使用len()函数查看上述字符串的长度时,输出为37。

但是,当我从文本文件中读取相同的字符串时,python会将\ UDD23或\ UD83E读取为单独的字符,即总共12个字符,这是我不想要的,因为我的encode()。decode()函数不会给出预期的结果。也就是说,unicode点不会转换为表情符号。我使用下面的代码

count=0
for item in enumerate(list(tweet_dict)):
    if item[0]==75:
        a=item[1]['text']
        print('Length of the string is: ',len(str(a)))
        print(a.encode('utf-16','surrogatepass').decode('utf-16'))
        

输出为:

Length of the string is:  47
Yes! Sardines in a can distancing! \uD83E\uDD23

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)