extracting a dictionary key and assigning it to a variable in python
<p>The file names.dat looks exactly like this:</p>

<pre><code>Fry, Philip J.;999 111-1111;999 222-2222  
Rodriguez, Bender B.;999 333-3333;999 444-4444  
Farnsworth, Professor;999 555-5555;999 666-6666  
Leela, Turanga;999 777-7777;999 888-8888  
Wong, Amy;999 999-9999;999 101-1010
</code></pre>

<p>And the below code gives the printed results down at the bottom. What I want to do is make a menu driven program that will allow the user to choose by number which record they want to modify. In pseudo code it would look something like this:</p>

<ul>
<li>The first block of code with the numbered phone listing goes here  </li>
<li>Enter by number the record you want to modify  </li>
<li>User enters 3  </li>
<li>For Rodriguez, Bender B enter a c to modify the cell phone or enter an h to modify the home phone  </li>
<li>User enters h  </li>
<li>Type in the 10 digit phone number with no spaces  </li>
<li>User enters 7771234567  </li>
<li>Record gets changed  </li>
<li>Enter p to view the modified phone book (which would show all the records but with a different phone for Bender  </li>
<li>Would you like to make another modification? y for yes, n for no</li>
</ul>

<p>My problem is that I can't find a way to bind the primary key to the entered user number. Being that python dictionaries have no order I'm finding this hard to do. Can someone please help? I only need that, the rest I can do myself.  </p>

<pre><code>pBook = {}  
fp = open('names.dat')  

# populate pBook  
while 1:  
    line = fp.readline()  
    if not line:  
        break  
x = line[:-1]  
data = x.split(';')  
pBook[data[0]] = {'home': data[1], 'cell': data[2]}  
fp.close()  

# optional if you want to see the dictionary structure   
# print pBook  

i = 1  
print '\nPhone listings:\n'  
for record in pBook:  
    key = str(i)  
    i += 1  
    print key + ')', record + ':', 'Home', pBook[record]['home'] + ',', 'Cell', pBook[record]['cell']  
print ''  
</code></pre>

<blockquote>
  <p>Phone listings:  </p>
  
  <ol>
  <li>Farnsworth, Professor: Home 999 555-5555, Cell 999 666-6666  </li>
  <li>Fry, Philip J.: Home 999 111-1111, Cell 999 222-2222  </li>
  <li>Rodriguez, Bender B.: Home 999 333-3333, Cell 999 444-4444  </li>
  <li>Wong, Amy: Home 999 999-9999, Cell 999 101-1010  </li>
  <li>Leela, Turanga: Home 999 777-7777, Cell 999 888-8888  </li>
  </ol>
</blockquote>

<p>Results of printing pBook after it has been populated, formatted for this post so it's easier understand its structure. The primary key is the person's name, the values is another dictionary where 'cell' and 'home' are keys and the phone numbers are the respective values.  </p>

<pre><code>{  
'Farnsworth, Professor': {'cell': '999 666-6666', 'home': '999 555-5555'},  
'Fry, Philip J.': {'cell ': '999 222-2222', 'home': '999 111-1111'},  
'Rodriguez, Bender B.': {'cell': '999 444-4444', 'home': '999 333-3333'},  
'Wong, Amy' : {'cell': '999 101-1010', 'home': '999 999-9999'},  
'Leela, Turanga': {'cell': '999 888-8888', 'home': '999 777-7777'}  
}  
</code></pre>
