2007年01月09日 星期二 17:05
ÈçÏ´úÂë import shelve fieldnames = ('name', 'age', 'job', 'pay') maxfield =max(len(field) for field in fieldnames) db = shelve.open('class-shelve') while True: key = raw_input('\nKey? => ') # key or empty line, exc at eof if not key: break try: record = db[key] # fetch by key, show in console except: print 'No such key "%s"!' % key else: for field in fieldnames: print field.ljust(maxfield), '=>', getattr(record, field) ºìÉ«ÄÇÒ»ÐУ¬ÔÚpython2.3ÖÐÎÞ·¨ÔËÐУ¬2.4Ôò¿ÉÒÔ¡£ÕâÖмäÓÐʲô¸Ä½øºÍ±ä»¯£¿ -- -------------------------------------------- ¶ÀÁ¢Ö®Ë¼Ï룬×ÔÓÉÖ®¾«Éñ¡£ --³ÂÒúã¡ -------------- 下一部分 -------------- Ò»¸öHTML¸½¼þ±»ÒƳý... URL: http://python.cn/pipermail/python-chinese/attachments/20070109/932bb0b2/attachment.html
2007年01月09日 星期二 17:18
On 1/9/07, 适兕 <lijiangsheng1 at gmail.com> wrote: > > 如下代码 > import shelve > fieldnames = ('name', 'age', 'job', 'pay') > maxfield =max(len(field) for field in fieldnames) > db = shelve.open('class-shelve') > > while True: > key = raw_input('\nKey? => ') # key or empty line, exc at eof > if not key: break > try: > record = db[key] # fetch by key, show in console > except: > print 'No such key "%s"!' % key > else: > for field in fieldnames: > print field.ljust(maxfield), '=>', getattr(record, field) > > > > 红色那一行,在python2.3中无法运行,2.4则可以。这中间有什么改进和变化? > > -- -------------------------------------------- > 独立之思想,自由之精神。 > --陈寅恪 > _______________________________________________ > python-chinese > Post: send python-chinese at lists.python.cn > Subscribe: send subscribe to python-chinese-request at lists.python.cn > Unsubscribe: send unsubscribe to python-chinese-request at lists.python.cn > Detail Info: http://python.cn/mailman/listinfo/python-chinese > generator expression -- http://codeplayer.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://python.cn/pipermail/python-chinese/attachments/20070109/6cdda218/attachment.html
Zeuux © 2025
京ICP备05028076号