i have this code that reads a file and strips out all the non essential stuff and then i want to make a dictionary for the actors and films they have been in .. i do that but it keeps on telling me that actors isnt defined ... how am i supposed to make it defined so that it will work ?
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('/Volumes/z/actors.short.list.txt')
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[0] == '-': 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[0] == '"':
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('/Volumes/z/actors.short.list.txt', print_info)
heres the code for the dictionary .. its all going to be in the same program.
dict1 = {}
k = 0
for x in actor:
dict1[x] = title [k]
k += 1
print dict1