Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 表情符号,当文本文件包含utf-8和utf-16时编码/解码_Python_Utf 8_Decode_Encode_Emoji - Fatal编程技术网

Python 表情符号,当文本文件包含utf-8和utf-16时编码/解码

Python 表情符号,当文本文件包含utf-8和utf-16时编码/解码,python,utf-8,decode,encode,emoji,Python,Utf 8,Decode,Encode,Emoji,我有一个文本文件,其中包含以下内容: .... {"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, "im

我有一个文本文件,其中包含以下内容:

....     
{"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}}
....
一些表情符号采用
\uhhh
格式,一些表情符号采用
\uhhhhh
格式

是否存在编码/解码以显示表情的方法?因为如果文件只包含
\uhhhhhhh
,那么一切正常

为了达到这个阶段,我对文件进行了如下修改:

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'))
你可以这么做

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

您可以这样做

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

这与UTF-8或UTF-16无关。这只是Python对Unicode字符进行转义的一般方法,U+FFFF下面的所有字符都使用
\uFFFF
,上面的所有字符都使用
\uffffff
(出于历史原因)

两个转义序列在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或UTF-16无关。这只是Python对Unicode字符进行转义的一般方法,U+FFFF下面的所有字符都使用
\uFFFF
,上面的所有字符都使用
\uffffff
(出于历史原因)

两个转义序列在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'

Python3也是如此。

好的。Python 2.7,赢得10分

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

读取文件并使用“unicode转义”进行解码:那么您就有了一个Python unicode字符串;让我们称之为
您的\u unicode\u字符串

要写入文件,请选择以下任一选项:

output_encoding = 'utf-8'

然后:

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版本,在没有任何篡改的情况下,您将无法将
打印到控制台并查看emojis。

确定。Python 2.7,赢得10分

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

读取文件并使用“unicode转义”进行解码:那么您就有了一个Python unicode字符串;让我们称之为
您的\u unicode\u字符串

要写入文件,请选择以下任一选项:

output_encoding = 'utf-8'

然后:

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版本,在没有任何篡改的情况下,您将无法将
打印到控制台并查看emojis。

这不是纯JSON文件,请将其视为文本,我需要在\uhhh和\uhhhhhh都被删除时对文件进行解码inside@NANA使用这个方法两者都会起作用。你先做json.loads。这不是纯json文件,请将其视为文本,我需要在\uhhh和\uhhhhhh都被删除时对文件进行解码inside@NANA使用这个方法两者都会起作用。并且您首先执行json.loads。UnicodeEncodeError何时显示?在Python控制台中执行
print
时?哪个python版本?哪个操作系统?@tzot当我尝试写入文件python 2.7、Win10时,UnicodeEncodeError何时出现?在Python控制台中执行
print
时?哪个python版本?哪个操作系统?@tzot当我尝试写入文件时,python 2.7,Win10最初,当我得到结果时,我将文件转换为utf-8格式,然后我修改它并将其写入导出文件,这会导致吗?最初,当我得到结果时,我将文件转换为utf-8格式,然后我修改它并将其写入导出文件,这会导致什么?最初的问题是BigQuery不接受\uhhhhhhh格式的表情符号,因为它只是作为文本打印,然后经过检查,BQ和表情符号没有问题,所以文件的问题(编码方式)。。。。我在这片森林里迷路了-\uuuuuuuuuuuuuuuuuuuuuuuuuuuuojis的格式是
\uhhhh\uhhh
\uhhhh\uhhhh
或者
\uhhhhhh\uhhhh
等等,然后我转换成了
\uhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
,它应该与谷歌,而且不是基于反馈,而是编码问题基本上是理想的,我需要在文本文件中而不是\uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh;使用类似于
utf-8
utf-16-le
的编码将此解码的unicode字符串保存在文本文件中,将存储实际的表情符号。这就是我上面的答案。最初的问题是BigQuery不接受\uhhhhhhh格式的表情符号,因为它只是作为文本打印,然后经过检查,BQ和表情符号没有问题,所以文件的问题(编码方式)。。。。我在这片森林里迷路了-\uuuuuuuuuuuuuuuuuuuuuuuuuuuuojis的格式是
\uhhhh\uhhh
\uhhhh\uhhhh
或者
\uhhhhhh\uhhhh
等等,然后我转换成了
\uhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
,它应该与谷歌,而且不是基于反馈,而是编码问题基本上是理想的,我需要在文本文件中而不是\uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh;使用类似于
utf-8
utf-16-le
的编码将此解码的unicode字符串保存在文本文件中,将存储实际的表情符号。这就是我上面的答案。