import os ## Move into the directory where the database is. os.chdir('DIRECTORYNAME') open_file = open('FILENAME.txt', 'r') lines = 0 person_end = 0 city_end = 0 state_end = 0 amount_end = 0 cityDict = {} user_info = raw_input('Type a name, city or address. Partial words are ok.\n') ### Find the number of donors in a city def find_city_number(): for line in open_file: if line.find(user_info) >=1: if cityDict.has_key(user_info): cityDict[user_info] += 1 else: cityDict[user_info] = 1 ### Get the list of donations from city. Definitions break the line out by field. def find_city_list(): open_file = open('FILENAME.txt', 'r') for line in open_file: person_end = (line.find(',')) person_name = line[:person_end] city_end = (line.find(',', person_end + 1)) city_name = line[(person_end + 1):city_end] state_end = (line.find(',', city_end + 1)) state_name = line[(city_end + 1):state_end] amount_end = (line.find(',', state_end + 1)) amount = line[(state_end + 1):amount_end] if line.find(user_info) >= 1: print person_name + ';', city_name + ',', state_name + ';', '$' + amount ## Run the program, taking one city name and giving the count and list find_city_number() if cityDict == {}: print 'No donors found.' else: print cityDict find_city_list()
You need to create an account or log in to post comments to this site.