Search code examples
pythonshelve

How to display all data in shelve in order?


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.


Solution

  • 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 ])