我有一个包含以下内容的文本文件:....
{"emojiCharts":{"emoji_icon":"\u2697","repost": 3, "doc": 3, "engagement": 1184, "reach": 6734, "impression": 44898}}
{"emojiCharts":{"emoji_icon":"\U0001f924","repost": 11, "doc": 11, "engagement": 83, "reach": 1047, "impression": 6981}}
....
有些表情符号是\uhhhh格式,有些是\Uhhhhhhhh格式。在
是否有任何方法可以对其进行编码/解码以显示表情符号?因为如果文件只包含\Uhhhhhhhh,那么一切正常。在
为了达到这个阶段,我用以下方式修改了文件:
^{pr2}$
要显示表情符号,我需要使用以下命令:insightData.decode("raw_unicode_escape").encode('utf-16', 'surrogatepass').decode('utf-16')
但它显示一个错误:UnicodeEncodeError: 'ascii' codec can't encode character u'\u2600' in position 30: ordinal not in range(128)
解决方案:with open(OUTPUT, "r") as infileInsight:
insightData = infileInsight.read()\
.decode('raw_unicode_escape')
with open(OUTPUT, "w+") as outfileInsight:
outfileInsight.write(insightData.encode('utf-8'))