Goodafter noon all,
i would like some help on how to modify a python program to do the following:
1. Read both files actors.small.list and actresses.small.list and, using a dictionary, build a database that maps from each actor to a list of his or her films.
2. Two actors are “co-stars” if they have been in at least one movie together. Process the database/dictionary you built in the previous step and build a second database that maps from each actor to a list of his or her costars.
3. Have the user enter the name of a movie star (potentially repeatedly). Your program should print both the list of films the star appeared in and the list of all the co-stars.
import os import re def process_file(filename, f): """read the given filename and extract information; for each film, call f() with string arguments: actor, date, title, role """ fp = open(filename) i = 0 # skip over the header until you get to the following magic line for line in fp: if line.strip() == '---- ------': break # regexp to recognize actor, tabs, movie split1 = re.compile('([^\t]*)\t*(.*)', re.UNICODE) # regexp to recognize title, date, role split2 = re.compile('([^\(]*)\s*(\([^\)]*\))[^\*(\[[^\]]*\])?', re.UNICODE) # regexp to recognize television (TV), video (V), video game (VG) video = re.compile('\(T?V|VG\)', re.U) actor = '' for line in fp: line = line.rstrip() if line == '': continue if line == '-': break # split the line into actor info and movie info; # keep track of the current actor ro = split1.match(line) if ro: new_actor, info = ro.groups() if new_actor: actor = new_actor else: print 'BAD1', line continue # skip television shows (titles in quotation marks) if info == '"': continue # skip made for TV and straight to video if video.search(info): continue # split the info into title, date and role ro = split2.match(info) if ro: title, date, role = ro.groups() if date == None: print 'BAD2', line continue f(actor, date, title, role) i += 1 else: print 'BAD3', line continue stat = fp.close() if __name__ == '__main__': def print_info(actor, date, title, role): print actor, date, title, role process_file('actors.short.list', print_info)
help would be appreciated as i am stuck on this for a very long time.