【问题标题】:Python print Unicode characterPython打印Unicode字符
【发布时间】:2015-06-23 19:07:57
【问题描述】:

我正在制作一款纸牌游戏,但我遇到了似乎是编码问题。我正在尝试打印这样的卡片:

def print(self):
    print("|-------|")
    print("| %s     |" % self.value)
    print("|       |")
    print("|   %s   |" % self.suit.encode("utf-8"))
    print("|       |")
    print("|    %s  |" % self.value)
    print("|-------|")

这就是我想要的:

|-------|
| 10    |
|       |
|   ♦   |
|       |
|    10 |
|-------|

...但这就是我得到的:

|-------|
| 10    |
|       |
|   b'\xe2\x99\xa6'   |
|       |
|    10 |
|-------|

如果重要的话,我使用的是 Windows 和 Python 3。

self.suit 的值可以是以下任意一种:

spade = "♠"
heart = "♥"
diamond = "♦"
club = "♣"

如果我删除 .encode("utf-8") 我会收到此错误:

Traceback(最近一次通话最后一次):

  File "main.py", line 79, in <module>
    start()
  File "main.py", line 52, in start
    play()
  File "main.py", line 64, in play
    card.print()
  File "main.py", line 36, in print
    print("|   \u2660   |")
  File "C:\Python34\lib\encodings\cp850.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2660' in position
4: character maps to <undefined>

【问题讨论】:

  • self.suit 的值是多少?你使用 Python 2 还是 3?
  • # -*- coding: utf8 -*- 作为我文件的第一行,并将self.value 更改为"9",并将self.suit.encode("utf-8") 更改为"♦",这对我构建它来说很好在 Sublime Text 3 中。如果你用你想要的字符替换 self.suit.encode 行会发生什么?
  • @maccartm 它在我的 IDE (PyCharm) 中有效,但在 windows 命令行中无效。
  • 这是一个 cmd 问题,是的。您的终端位于代码页 850 中,因此您无法打印 ♦ ,它在 850 中不存在。代码页 65001 可以工作,除非它在 ​​Windows 中存在实现错误。一般而言,Windows 命令行对于 Unicode 来说是一个致命的损失。

标签: python unicode


【解决方案1】:

这利用了 Windows 控制台中的 OEM 代码页打印一些可见字符作为控制字符的事实。 cp437cp850 的牌组是 chr(3)-chr(6)。 Python 3(3.6 之前)不会打印黑色菱形的 Unicode 字符,但它是 U+0004 得到的:

>>> print('\N{BLACK DIAMOND SUIT}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python33\lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u2666' in position 0: character maps to <undefined>
>>> print(chr(4))
♦

因此:

#!python3
#coding: utf8
class Card:
    def __init__(self,value,suit):
        self.value = value
        self.suit = suit    # 1,2,3,4 = ♥♦♣♠

    def print(self):
        print("┌───────┐")
        print("| {:<2}    |".format(self.value))
        print("|       |")
        print("|   {}   |".format(chr(self.suit+2)))
        print("|       |")
        print("|    {:>2} |".format(self.value))
        print("└───────┘") 

输出:

>>> x=Card('K',4)
>>> x.print()
┌───────┐
| K     |
|       |
|   ♠   |
|       |
|     K |
└───────┘
>>> x=Card(10,3)
>>> x.print()
┌───────┐
| 10    |
|       |
|   ♣   |
|       |
|    10 |
└───────┘

Python 3.6 更新

Python 3.6 使用 Windows Unicode API 进行打印,因此现在不需要控制字符技巧,并且可以使用新的格式字符串:

#!python3.6
#coding: utf8
class Card:
    def __init__(self,value,suit):
        self.value = value
        self.suit = '♥♦♣♠'[suit-1] # 1,2,3,4 = ♥♦♣♠

    def print(self):
        print('┌───────┐')
        print(f'| {self.value:<2}    |')
        print('|       |')
        print(f'|   {self.suit}   |')
        print('|       |')
        print(f'|    {self.value:>2} |')
        print('└───────┘') 

输出:

>>> x=Card('10',3)
>>> x.print()
┌───────┐
| 10    |
|       |
|   ♣   |
|       |
|    10 |
└───────┘

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2018-06-22
    • 2017-02-16
    • 2013-07-16
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多