[Tutor] Handling Unpickled Data

Don Parris gnumathetes at gmail.com
Tue Jul 5 23:10:03 CEST 2005


I have a script that gets a little info from the user, using a
dictionary, which I then store in a file via cPickle.  This pickle
concept must be good for something, but I haven't figured out exactly
what just yet.  At any rate, I would like to now get the code and be
able to display only one value for the user.

I thought I could do this by specifying a key in orginfo.  However,
the function throws all the data to stdout, not just the desired key. 
How can I limit the output to just the value of the key I want to
reference?  Do I need to create a for or while loop, or an if
statement to say, "extract this key:value pair only"?

I can also get this from a SQL query, but since I couldn't do it with
a file, I wanted to try and figure it out.  The book, "Learning
Python" is very helpful, now that I have it, but it kind of breezed
past this kind of thing.  I really would like to understand how to
work with the data in the text file.

### function def ###
def org_Get_Info():
    orgGdata = open('orgdata.txt', 'r')
    orginfo = cPickle.load(orgGdata),
    print orginfo['Name']                  # this is really all I want.

### Output at DOS prompt ###
Organization Name: ocf          # Begin input
Address: 6123
City: char
State: nc
Zip: 282
City :  char           # Begin output from org_Get_Info()
State :  nc
Name :  ocf
Zip :  282
Address :  6123
ocf                     # this is all I want - not the rest.

DC Parris GNU Evangelist
gnumathetes at gmail.com
Free software is like God's love - 
you can share it with anyone anywhere anytime!

More information about the Tutor mailing list