简体   繁体   English

Unicode Python Windows控制台

[英]Unicode Python Windows Console

I have a program for a Blackjack game. 我有一个21点游戏的程序。 It is not completed but I am having an issue with Unicode. 还没有完成,但是我遇到了Unicode问题。 When it prints a string it is not in the correct format (Look Below). 当它打印一个字符串时,它的格式不正确(请看下面)。 It is Python 3. 是Python 3。

from unicurses import *
import sys
import time
from random import choice as rc
import random

def total(hand): #total
    aces = hand.count(11)
    t = sum(hand)
    if t > 21 and aces > 0:
        while aces > 0 and t > 21:
            t -= 10
            aces -= 1
    return t

def random(one,two):
    import random
    number = random.randint(one,two)
    return number

def number():
    number = random(1,13)
    if number == 13:
        value = '0'
    elif number == 12:
        value = '0'
    elif number == 11:
        value = '0'
    elif number == 10:
        value = '0'
    elif number < 10:
        value = number
    return value

def card():
    cardnumber = number()
    card = cardnumber
    return card

def store10Cards():
    tenCards = [card(),
               card(),
               card(),
               card(),
               card(),
               card(),
               card(),
               card(),
               card(),
               card()]
    return tenCards

stdscr = initscr()
cards = store10Cards()

mvaddstr(10, 10, '┌──────────┐\n│{}         │\n│           │\n│            │\n│            │\n│            │\n│            │\n│            │\n│            │\n│         {}│\n└──────────┘'.format(cards[0], cards[0]).encode('utf-8'))


#deck = [] #deck of cards

#cwin = 0 #computer wins
#pwin = 0 #player wins

#for i in range (2, 11):
   # for j in range ([24, 72][i == 10]):
       # deck.append(i)

#stdscr = initscr() #Start the standard screen 

start_color() #Start the color service

init_pair(1, COLOR_RED, 0) #color pair 1 is red text and no highlighting
init_pair(2, COLOR_WHITE, 0) #color pair 2 is white text and no highlighting

addstr('\n\n------------------------------------------------------------------------', color_pair(1) | A_BOLD)
addstr('\n\n\nAre You Ready To Try And Defeat The Undefeated Blackjack Master?\n\n\nThe First One To Win 10 Hands Wins!!!\n\n\nAces Are Counted As 11s And Automaticlly Change To 1 If You Go Over 21. \n\n\nWinning 1 Hand Does Not Mean You Win The Game\n\n\nIt Is Possible To Win But Very Hard!!!                  Good Luck!!!\n\n\n', color_pair(2))
addstr('------------------------------------------------------------------------', color_pair(1) | A_BOLD)
addstr('\n\nDo You Know The Rules Of Blackjack? (Y or N)\n\n', color_pair(2))
addstr('------------------------------------------------------------------------', color_pair(1) | A_BOLD)

refresh() #refreshes

cbreak() #lets user do Ctrl + C

noecho() #Stops inputs from being shown

a = getch() #Gets input

while a == ord('q'): #Checks if input is q
    clear() #clears before exit
    exit() #quits

while a == ord('n'): #Checks if input is n

    clear() #clears before displaying the next text

    addstr('\n\n\n----------------------------------------------------------------------------------', color_pair(1) | A_BOLD)
    addstr('\n\nOk. That Is Ok. The Rules Are As Follows: \n\nThe Goal Is To Get To 21.\n\nYou Can Hit A Card (Receive Another) Or Stand And See What The Dealer Has And Does.\n\nThe Person With The Higest Value 21 Or Below Wins That Hand.\n\nAces Start As 11s But Change Automatticly To 1s If You Go Over 21 To Save You.\n\nIf You Get 21 Exactly You Get A Blackjack.\n\nA Blackjack Means You Automaticly Win Unless The Computer Gets A 21 And Ties You.\n\nBe Careful Not To Hit Too Many Times Because If You Go Over 21 You Lose That Hand.\n\nAny Face Card Is Automaticly A 10 At All Times.\n', color_pair(2))
    addstr('\n----------------------------------------------------------------------------------\n\n\n', color_pair(1) | A_BOLD)

    sleep(15)

    a = getch() #resets input variable

    while a == ord('q'): #Checks if input is q
        clear() #Clears before exit
        exit() #quits

while a == ord('y'): #Checks if input is y

    clear() #clears before displaying the next text

    addstr('\n\n\n\n\n----------------------------------------------------------------------------------', color_pair(1) | A_BOLD)
    addstr('\n\n\n\nGreat!\n\n\n\nHave Fun!\n\n\n\nGood Luck!\n\n\n', color_pair(2))
    addstr('----------------------------------------------------------------------------------\n\n\n', color_pair(1) | A_BOLD)

    refresh()

    time.sleep(1)

    b = '1'

    a = '' #resets input variable

Here is the line I am having issues with: 这是我遇到的问题所在的行:

mvaddstr(10, 10, '┌──────────┐\n│{}         │\n│           │\n│            │\n│            │\n│            │\n│            │\n│            │\n│            │\n│         {}│\n└──────────┘'.format(cards[0], cards[0]).encode('utf-8'))

I tried to encode it with UTF-8 and set Command Prompt to UTF-8 by doing chcp then when I run that program that string prints: 我尝试使用UTF-8对其进行编码,并通过执行chcp将命令提示符设置为UTF-8,然后在运行该程序时显示字符串:

b'\\xe2\\x94\\x8c\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\x e2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x90\\n\\xe2\\x94\\x823 \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2 \\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\x e2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 \\t \\xe2\\x94\\x82\\n\\xe2\\x94\\x82 3\\xe2\\x94\\x82\\n\\xe2\\x94\\x94\\xe2\\x94\\x80\\xe 2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x80\\xe2\\ x94\\x80\\xe2\\x94\\x80\\xe2\\x94\\x98' b'\\ xe2 \\ x94 \\ x8c \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ x e2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x90 \\ n \\ xe2 \\ x94 \\ x823 \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ x e2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 \\ t \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x82 3 \\ xe2 \\ x94 \\ x82 \\ n \\ xe2 \\ x94 \\ x94 \\ xe2 \\ x94 \\ x80 \\ xe 2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x80 \\ xe2 \\ x94 \\ x98'

Please help me print the string like it should be and not be in unicode. 请帮助我按原样打印字符串,而不要使用unicode。

If you have any other suggestions on my code let me know. 如果您对我的代码有其他建议,请告诉我。

On Windows, unicurses uses ctypes to wrap PDCurses . 在Windows上,unicurses使用ctypes包装PDCurses This library is optionally built with Unicode support in the 32-bit DLLs that are distributed on the project site. 该库可以选择在项目站点上分发的32位DLL中使用Unicode支持来构建。 FYI, the Unicode versions call console wide-character APIs such as WriteConsoleOutputW , so don't worry about the legacy console codepage. 仅供参考,Unicode版本调用控制台宽字符API,例如WriteConsoleOutputW ,因此不必担心旧版控制台代码页。 You need the "pdc34dllu.zip" build, which takes UTF-8 encoded parameters. 您需要“ pdc34dllu.zip”构建,该构建采用UTF-8编码的参数。 Because the pre-built DLLs are only 32-bit, you'll have to use 32-bit Python, unless you have Visual Studio 2015 installed to build a 64-bit DLL. 因为预构建的DLL只有32位,所以除非您安装了Visual Studio 2015才能构建64位DLL,否则必须使用32位Python。

You'll also have to make a couple of changes to unicurses to make this work. 为了使这项工作,您还必须对单点进行一些更改。 Here are the steps I followed to get this working for 32-bit Python 3.5: 这是我为32位Python 3.5进行此操作所遵循的步骤:

  1. Download pdc34dllu.zip . 下载pdc34dllu.zip Extract "pdcurses.dll" to either Python's installation directory, ie where python.exe is located, or to a directory that's in the PATH environment variable. 将“ pdcurses.dll”解压缩到Python的安装目录(即python.exe所在的位置)或PATH环境变量中的目录中。
  2. Download and extract UniCurses-1.2.zip . 下载并解压缩UniCurses-1.2.zip
  3. Before installing, make the following changes to unicurses.py. 在安装之前,请对unicurses.py进行以下更改。 On line 34 change code = locale.getpreferredencoding() to code = 'utf-8' . 在第34行,将code = locale.getpreferredencoding()更改为code = 'utf-8' Next, comment out lines 52-54, which start with if not os.access("pdcurses.dll",os.F_OK) because this check will always fail if "pdcurses.dll" isn't in the working directory. 接下来,注释掉第52-54行,该行以if not os.access("pdcurses.dll",os.F_OK)因为如果“ pdcurses.dll”不在工作目录中,则此检查将始终失败。
  4. Install the package by running python setup.py install . 通过运行python setup.py install软件包。

example: printing a card 示例:打印卡

import unicurses

card_template = '\n'.join([
    '┌────────────┐',
    '│{card}           │',
    '│            │',
    '│            │',
    '│            │',
    '│            │',
    '│            │',
    '│            │',
    '│            │',
    '│           {card}│',
    '└────────────┘'])

def main():
    stdscr = unicurses.initscr()
    unicurses.clear()
    card_string = card_template.format(card=5)
    for i, line in enumerate(card_string.splitlines()):
        unicurses.mvaddstr(5 + i, 5, line)
    unicurses.refresh()
    unicurses.endwin()

if __name__ == '__main__':
    main()

output 产量

     ┌────────────┐
     │5           │
     │            │
     │            │
     │            │
     │            │
     │            │
     │            │
     │            │
     │           5│
     └────────────┘

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM