【问题标题】:Emoji, encode/decode when text file contain utf-8 and utf-16表情符号,当文本文件包含 utf-8 和 utf-16 时编码/解码
【发布时间】:2019-02-11 15:35:20
【问题描述】:

我有一个包含以下内容的文本文件:

....     
{"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 则一切正常。

到这个阶段我已经这样修改了文件:

insightData.decode("raw_unicode_escape").encode('utf-16', 'surrogatepass').decode('utf-16').encode("raw_unicode_escape").decode("latin_1")

要显示表情符号,我需要使用这个:

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'))

【问题讨论】:

  • UnicodeEncodeError 什么时候出现?在 Python 控制台中执行 print 时?哪个python版本?哪个操作系统?
  • @tzot 当我尝试写入文件时,python 2.7,WIN10

标签: python utf-8 decode encode emoji


【解决方案1】:

你可以这样做。

print a["emojiCharts"]["emoji_icon"].decode("unicode-escape")

输出:

【讨论】:

  • 这不是纯JSON文件,请把它当作文本,当\uhhh和\Uhhhhhhhh都在里面时我需要解码文件
  • @NANA 使用这种方法都可以。你先做 json.loads。
【解决方案2】:

这与 UTF-8 或 UTF-16 无关。这只是 Python 转义 Unicode 字符的一般方式,U+FFFF 以下的所有内容都使用\uFFFF,而以上的所有内容都使用\UFFFFFFFF(出于历史原因)。

两个转义序列在 Python 字符串中的工作方式应该完全相同。在我的机器上,使用@vks 的解决方案:

$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34)
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '\U0000ABCD'.decode('unicode-escape')
u'\uabcd'
>>> '\uABCD'.decode('unicode-escape')
u'\uabcd'

对于 Python 3 类似。

【讨论】:

  • 最初,当我得到一个结果时,我将文件转换为utf-8格式,然后我修改它并将其写入导出文件,会不会导致?
【解决方案3】:

好的。 Python 2.7,Win 10。

您的原始文件是纯 ASCII 码,包含精确的 unicode 转义(“\u####”,6 个字节和“\U########”,10 个字节)。

读取文件并使用 'unicode-escape' 解码:然后你就有了一个 Python unicode 字符串;我们就叫它your_unicode_string吧。

要写入文件,请选择:

output_encoding = 'utf-8'

output_encoding = 'utf-16-le'

然后:

import codecs
with codecs.open(output_filename, 'w', encoding=output_encoding) as fpo:
    # fpo.write(u'\ufeff') # for windows, you might want to write this at the start
    fpo.write(your_unicode_string)

对于您给定的 python 和 os 版本并且没有任何篡改,您将无法仅在控制台上 print 并查看表情符号。

【讨论】:

  • stackoverflow.com/questions/52199674/… 最初的问题是 BigQuery 不接受 \Uhhhhhhhh 格式的表情符号,因为它只是打印为文本,然后经过检查,可以查看 BQ 和表情符号没有问题,所以问题使用文件,(它的编码方式)....我只是迷失在这片森林中-___-来自API emojis的格式为\uhhhh\uhhhh\uhhhh\uhhhh\uhhhh\uhhhh等等,然后我转换为\Uhhhhhhhh 假设与 goolge BQ 一起使用,而不是基于反馈它是编码问题
  • 基本上理想情况下我需要在文本文件中而不是 \Uhhhhhhhh 代码将是实际的表情符号,这可能吗??
  • 你读取输入文件并使用raw_unicode_escape解码;使用utf-8utf-16-le 之类的编码将此解码后的unicode 字符串保存在文本文件中,将存储实际的表情符号。这就是我上面的回答所做的。
猜你喜欢
  • 1970-01-01
  • 2013-08-04
  • 2020-11-07
  • 1970-01-01
  • 2016-06-10
  • 2021-04-04
  • 1970-01-01
  • 2017-05-23
  • 2014-01-18
相关资源
最近更新 更多