【问题标题】:Python 3 array with UTF-16 chars带有 UTF-16 字符的 Python 3 数组
【发布时间】:2018-08-07 07:13:51
【问题描述】:

我使用表情符号的 unicode 值创建了两个包含正面和负面表情符号的数组:

positive = [
u'\U0001F600',
u'\U0001F601',
u'\U0001F602',
u'\U0001F923',
u'\U0001F603',
u'\U0001F604',
u'\U0001F605',
u'\U0001F606',
u'\U0001F609',
u'\U0001F60A',
u'\U0001F60B',
u'\U0001F60E',
u'\U0001F60D',
u'\U0001F618',
u'\U0001F617',
u'\U0001F619',
u'\U0001F61A',
u'\U0000263A',
u'\U0001F642',
u'\U0001F917',
u'\U0001F60F',
u'\U0001F60C',
u'\U0001F61B',
u'\U0001F61C',
u'\U0001F61D',
u'\U0001F924',
u'\U0001F643',
u'\U0001F62C']

negative = [
u'\U0001F610',
u'\U0001F611',
u'\U0001F636',
u'\U0001F644',
u'\U0001F60F',
u'\U0001F623',
u'\U0001F625',
u'\U0001F62E',
u'\U0001F910',
u'\U0001F62F',
u'\U0001F62A',
u'\U0001F62B',
u'\U0001F634',
u'\U0001F612',
u'\U0001F613',
u'\U0001F614',
u'\U0001F615',
u'\U0001F641',
u'\U0001F616',
u'\U0001F61E',
u'\U0001F61F',
u'\U0001F624',
u'\U0001F622',
u'\U0001F62D',
u'\U0001F626',
u'\U0001F627',
u'\U0001F628',
u'\U0001F629',
u'\U0001F630',
u'\U0001F631',
u'\U0001F635',
u'\U0001F621',
u'\U0001F620',
u'\U0001F637',
u'\U0001F912',
u'\U0001F915',
u'\U0001F922',
u'\U0001F927']

但是,例如,当我打印 positive[0] 时,我得到的是这个奇怪的字符而不是表情符号:

我正在使用 Amazon Linux 和 python-3.4 开发 EC2 机器。 在我的 Macbook 上,相同的代码可以正常工作。

【问题讨论】:

  • 我得到一个表情符号。
  • 在 macOS 终端上也适用于我。可能与您使用的控制台模拟器有关。
  • 这取决于最终用户环境能够解释 unicode 集。特定字体可能没有代表预期字符的字符,因此显示默认字符(即带问号的菱形)。
  • 问题出在“屏幕”上,如果我不在它下面运行,它可以正常工作。

标签: python python-3.x utf-16


【解决方案1】:

你好Daniel Haviv

如果可行,试试这个:

print(positive[0].encode('utf-8'))

您可以在这里阅读更多内容Python 2.x’s Unicode Support

更新:之前的解决方案不适合你试试这个:

import sys
import codecs
sys.stdout = codecs.getwriter('utf8')(sys.stdout)

print(positive[0])

我从Setting the correct encoding when piping stdout in Python找到了解决方案

【讨论】:

  • 我使用的是 Python 3 和 UTF-16。编码('utf-8')返回:\xf0\x9f\x91\x8d'
  • @DanielHaviv,Python 3 具有相同的语法。 UTF-16 是什么意思?你的意思是你试过print(positive[0].encode('UTF-16'))
【解决方案2】:

问题是我在screen 下运行

【讨论】:

  • 你好丹尼尔哈维夫。您能否再运行一个我添加的更新代码以检查屏幕。将来参考知道会很好。 BR
【解决方案3】:

该问题不是 Python 问题。 Mac 支持终端中可以打印这些 un​​icode 字符的字体。

在您使用的情况下,不支持这些相同的字体。

如果有问题的设备支持这些 un​​icode 值,它们会正确打印。

我在 Ubuntu 上测试了一个标准的 macOS SSH 终端,它的工作原理与本机 Mac 一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    • 2011-03-09
    • 1970-01-01
    • 2010-10-16
    相关资源
    最近更新 更多