Python 从数据库打印列表

Python 从数据库打印列表,python,list,Python,List,这是一个叫做电话的程序: import shelve import string UNKNOWN = 0 HOME = 1 WORK = 2 FAX = 3 CELL = 4 class phoneentry: def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN): self.name = name self.number = number

这是一个叫做电话的程序:

 import shelve
import string

UNKNOWN = 0
HOME = 1
WORK = 2
FAX = 3
CELL = 4

class phoneentry:
    def __init__(self, name = 'Unknown', number = 'Unknown', type = UNKNOWN):
            self.name = name
            self.number = number
            self.type = type

    #  create string representation
    def __repr__(self):
            return('%s:%d' % ( self.name, self.type ))

    #  fuzzy compare or two items
    def __cmp__(self, that):
            this = string.lower(str(self))
            that = string.lower(that)
            if string.find(this, that) >= 0:
                return(0)

            return(cmp(this, that))

    def showtype(self):
            if self.type == UNKNOWN: return('Unknown')
            if self.type == HOME: return('Home')
            if self.type == WORK: return('Work')
            if self.type == FAX: return('Fax')
            if self.type == CELL: return('Cellular')

class phonedb:

    def __init__(self, dbname = 'phonedata'):
            self.dbname = dbname;
            self.shelve = shelve.open(self.dbname);


    def __del__(self):
            self.shelve.close()
            self.shelve = None

    def add(self, name, number, type = HOME):
            e = phoneentry(name, number, type)
            self.shelve[str(e)] = e


    def lookup(self, string):
            list = []
            for key in self.shelve.keys():
                    e = self.shelve[key]
                    if cmp(e, string) == 0:
                            list.append(e)


            return(list)

#  if not being loaded as a module, run a small test
#if __name__ == '__main__':
        #foo = phonedb()
        #foo.add('Sean Reifschneider', '970-555-1111', HOME)
        #foo.add('Sean Reifschneider', '970-555-2222', CELL)
        #foo.add('Evelyn Mitchell', '970-555-1111', HOME)

        #print 'First lookup:'
        #for entry in foo.lookup('reifsch'):
                #print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() )
                #print

                #print 'Second lookup:'

        #for entry in foo.lookup('e'):
            #print '%-40s %s (%s)' % ( entry.name, entry.number, entry.showtype() )
我不得不接受这个程序并对其进行修改以使其更好。我已经能够完成一切,但打印一个数据库列表,其中包含添加了各自信息的每个人。有没有人能帮助我或者让我朝着正确的方向前进…见下面:我的代码

   from Telephone import *


pb = phonedb()

while (1):
    print "Welcome to the Telephone Database!"
    print
    print "Please make a selection:"
    print
    print "1) Add new number"
    print
    print "2) Find a number"
    print
    print "3) list all in the directory "
    print
    print "E) exit"
    print
    myinput = raw_input(':')
    if myinput == "1":
        print "you pushed 1"
    if myinput == "2":
        print "you pushed 2"
    if myinput == "3":
        print "you pushed 3"

    print
    if myinput == "1":

        print "What is the name of the person you want to add? :"
        p = raw_input(':')
        print "What is the number you want specified for this person? :"
        n = raw_input(':')
        print "What type of communication device is this : (0 = UNKNOWN, 1 = HOME, 2 = WORK, 3 = FAX, 4 = CELL)"
        t = raw_input(':')
        if t == '0':
            pb.add(p, n, UNKNOWN)
        if t == '1':
            pb.add(p, n, HOME)
        if t == '2':
            pb.add(p, n, WORK)
        if t == '3':
            pb.add(p, n, FAX)
        if t == '4':
            pb.add(p, n, CELL)

    if myinput == "2":

        print "Type the name of the person whos number you are looking for :"
        search = raw_input(':')
        for entry in pb.lookup(search):
            print '%-40s %s (%s)' % (entry.name, entry.number, entry.showtype())

    if myinput == "3":   # THIS IS WHERE I NEED ASSISTANCE...********
        for entry in pb.lookup(
            print '%-40s %s (%s)' % (enter.name, entry.number, entry.showtype())





    if myinput == "e" or myinput == "E":
        break

print "Thanks for playing -- El Gooey"

您需要使用比
cmp()更复杂的工具;请尝试传递
“*”
以查找所有条目。您可能还想看看
fnmatch.filter()

我不熟悉fnmatch()。知道如何去除列表中的重复项吗?这与打印列表有什么关系?@ignacio我错了。当我能够打印目录列表时,有几个副本。。。只是想弄清楚如何在cmp()函数中去掉这些。