unicode in python - Printable Version
-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: unicode in python (/showthread.php?tid=82860)
unicode in python by Jarrod on 04-01-2008 at 08:31 AM
code: class card(object):
heart = u"\2665"
dimo = u"\u2666"
spade = u"\u2664"
club = u"\u2663"
""" A playing card. """
RANKS = ["A", "2", "3", "4", "5", "6", "7",
"8", "9", "10", "J", "Q", "K"]
SUITS = [heart, dimo, club, spade]
def __init__(self, rank, suit,): ####defines suits and values
self.rank = rank
self.suit = suit
def __str__(self): ####define display string
rep = self.rank + self.suit
return rep
class hand (object):
"""A Hand of blackjack cards"""
def __init__(self):
self.cards = []
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + " "
else:
rep = "<empty>"
return rep
def clear(self):
self.cards = [ ]
def add(self, card):
self.cards.append(card)
def give(self, card, other_hand):
self.cards.remove(card)
other_hand.add(card)
class deck(hand):
""" a nice shiny new shiny deck of cards"""
def pop(self): ####lets fill this deck
for suit in card.SUITS:
for rank in card.RANKS:
self.add(card(rank, suit))
def shuffy(self): ###suffle the deck. thank god for random.py
import random
random.shuffle(self.cards)
def deal(self, hands = 2, per_hand = 1): ###get cards out of the deck into my hand
for rounds in range(per_hand):
for hand in hands:
if self.cards:
top_card = self.cards[0]
self.give(top_card, hand)
else:
print "the deck has ended"
class priv_card(card):
"""a cards that cant be seen """
def __str__(self):
return "!!unprintable!!"
class pos_card(card):
"""a card which is face up or down, but either up or down"""
def __init__(self, rank, suit, face_up = True):
super(pos_card, self).__init__(rank, suit)
self.is_face_up = face_up
def __str__(self):
if self.is_face_up:
rep = super(pos_card, self).__str__()
else:
rep = "XX"
return rep
def flip(self):
self.is_face_up = not self.is_face_up
##############################################
dek = deck() ##############make the dek
dek.pop() ##############fill the dek
dek.shuffy() ##############shuffle the dek
hnd1 = hand() ##############make hand 1
hnd2 = hand() ##############make hand 2
###define hands
hands = [hnd1, hnd2]
##################################################
#################################################
#populate hands
#################################################
dek.deal(hands, per_hand = 2)
#################################################
print "hand1: ", hnd1
print "hand2: ", hnd2
print "remaining cards:\n", dek
##test loop
#for item in dek.cards:
# print item
####
#############################test loop#################
######################################prints the dek###
#for item in dek.cards:
# print item
#card1 = card(rank = "A", suit = "c")
#hand_me = hand()
#print hand_me
#hand_me.add(card1)
#print hand_me
quote: Originally posted by python idle
hand1:
Traceback (most recent call last):
File "J:\_mycards1.py", line 125, in ?
print "hand1: ", hnd1
File "J:\_mycards1.py", line 40, in __str__
rep += str(card) + " "
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2663' in position 1: ordinal not in range(128)
so how do i get it to print the unicode characters???
it works if you don't use unicode and use a letter corrosponding to the suit but it looks better this way
RE: unicode in python by somelauw on 04-01-2008 at 12:16 PM
Use repr() on the unicodestring instead of str().
RE: unicode in python by Jarrod on 04-02-2008 at 06:15 AM
do you mean
quote: Originally posted by Jarrod
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += str(card) + " "
to
def __str__(self):
if self.cards:
rep = ""
for card in self.cards:
rep += repr(card) + " "
?
------------------
i replaced all the strs with repr s but no luck
RE: unicode in python by somelauw on 04-02-2008 at 11:58 AM
Sorry. When I replaced str by repr, it showed it the way the standard repr would show it, but that's not what you want.
I also noticed that in the python shell unicode strings work easier than in scripts. In the shell only str(unicode) doesn't work.
Please let me know when you fix it.
|