#!/bin/env python # Example of dictionaries and JSON files import random import sys import json def mkTelDict(n,names): """Build a dictionary of random telephone numbers for random first-names taken from .""" tel_dict = dict(); for i in range(n): no = random.randint(1000,9999) name = names[random.randint(0,99)] tel_dict[name]=no return tel_dict def ppTelDict(tel): """Pretty print a phone dictionary.""" for k, v in tel.items(): print(k, " -> ", v) def printNoOf(name,tel): """Print phone number of in dictionary , or sorry message.""" if name in tel: print("The tel no. of "+name+" is ", tel[name]) else: print("No phone number for "+name+", sorry!") def readFile(fname): """Read contents of a file, and put each line of the file into an element of a list.""" fd = open(fname,'r') names = [] for line in fd: names.append(line[0:-1]) return names # ----------------------------------------------------------------------------- # Constants fname = 'names.txt' jfile = 'tel.json' # ----------------------------------------------------------------------------- # main if (len(sys.argv) != 2): # expect 1 args: n print("Usage: dict3.py \n build a phone dictionary with entries and write it to a JSON file") else: n = int(sys.argv[1]) # read from command-line names = readFile(fname) tel = mkTelDict(n, names) ppTelDict(tel) json.dump(tel, fp=open(jfile,'w'), indent=2) print("Data has been written to file ", jfile); tel_new = json.loads(open(jfile,'r').read()) ppTelDict(tel_new) the_name = "Billy" printNoOf(the_name,tel_new); the_name = names[random.randint(0,99)]; printNoOf(the_name,tel_new);