4

This is the first time I post in stack overflow. Perhaps I can get the solution that I need.

busdata=shelve.open("Database")
for lctno in busdata.keys():
    outputLine( lctno , busdata[ lctno ])

It randomly display the data in my .dat file. I want it to display in ascending order.

martineau
  • 119,623
  • 25
  • 170
  • 301
Triple K
  • 93
  • 2
  • 5

2 Answers2

7

As g.d.d.c suggested, the solution is to sort the keys for yourself.

busdata=shelve.open("Database")
my_keys = list(bustdata.keys())
my_keys.sort()
for lctno in my_keys:
    outputLine( lctno , busdata[ lctno ])
ya24
  • 490
  • 1
  • 4
  • 16
Geoff Crompton
  • 440
  • 1
  • 5
  • 15
2

Since the code above did not work for me - I made some tests, and thought of posting my final working result (in full) - solving the listing of all data in shelve in order:

#!/usr/bin/python
# 

import shelve

def main():
    db = shelve.open("database.db")
    dkeys = list(db.keys())
    dkeys.sort()
    for x in dkeys:
        print ( x , db[ x ])
    db.close()
    return

if __name__ == "__main__":
    main()