Hi,

I have a class for Cards, that the suits and ranks are placed in

suitList = ("Hearts","Diamonds"...
rankList = ("","Ace","2"...

however I need to read these values from a file instead.

Once I read the file I need to be able to sort the suits and ranks then print the list of cards.

Any help would be appreciated.

Macca

I have made it this far. Yet it doesn't quite work. I'm a bit stuck...

The file i am loading looks like this:

2 Hearts
3 hearts
4 Hearts
...

import string
class Card:
    # Not sure if I need to set these first???
    # suitList = ()
    # rankList = ()
              
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
    def __repr__(self):
        return str(self)
    def __str__(self):
        return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
    def __cmp__(self,other):
        if self.suit > other.suit: return 1
        if self.suit < other.suit: return -1
        if self.rank > other.rank: return 1
        if self.rank < other.rank: return -1
        return 0
    
def getCard(cards):
    # Break each card into suit and rank
    rankList , suitList = string.split(cards," ")
    return Card(rankList,suitList)
class Deck:
    def __init__(self):
        self.cards = []
        for suit in range(4):
            for rank in range(1,14):
                self.cards.append(Card(suit,rank))
 
def main():
    # Open the file with the cards in them
    filename = raw_input('Enter the file name for the cards >>')
    infile = open(filename,'r')
    # Set the first card in the list
    cards = getCard(infile.readline())
    # Process the extra lines
    for line in infile:
         s = getCard(line)
    infile.close()
         
    a=Deck()
    print a
main()

Edit: added code tags --> vegaseat

You could handle your cards in the form of a list of (rank, suit) tuples which can be sorted easily by rank or suit, just take a look at the Python code snippet at:
http://www.daniweb.com/code/snippet568.html

I assume your card file is a limited number of cards, not the whole deck?

You could handle your cards in the form of a list of (rank, suit) tuples which can be sorted easily by rank or suit, just take a look at the Python code snippet at:
http://www.daniweb.com/code/snippet568.html

I assume your card file is a limited number of cards, not the whole deck?

In most of the examples it gives the data in a format like:

rank =["A","2",...
suit =["Clubs"...

yet I want to read the data from a file like

2 h
3 h
...
and then have the program change any h to hearts etc.

I have done the following:

f=open(file.txt,'r')
lines=f.readlines()


pack=[]
for i in lines:
pack.append(i.split())

[,...

so its not quite what I need.

How do I get the same format as the example above into the suit and rank split???

I have posted my full code below:

import string

f = open('C:\cs.txt','r').read()

class Card:
    suitList = []
    rankList = []
    
    def __init__(self,suit,rank):
        self.suit = suit
        self.rank = rank
        
    def __repr__(self):
        return str(self)
    
    def __str__(self):
        return "%s of %s" % (self.rankList[self.rank],self.suitList[self.suit])
    
class Deck:
    def __init__(self):
        self.cards = []
        for suit in range(4):
            for rank in range(0,13):
                self.cards.append(Card(suit,rank))

    def printDeck(self):
        for card in self.cards:
            print card

    def __str__(self):
        s = ""
        for i in range(len(self.cards)):
            s = s + "" + str(self.cards[i]) + '\n'
        return s


def main():
    f=open(cs.txt,'r')
    lines=readlines()
    f.close()

    pack=[]
    for i in lines:
        pack.append(i.split())
    
    Card.suitList = [1::2]
    Card.rankList = [0::2]
    deck1=Deck()
    print deck1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.