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

Where is your code for the dictionary actually located?

I think the problem you are having is that you are trying to access a variable that is out of scope (and therefore doesn't exist). If you are trying to access the variable actor in the function process_file, you will not be able to because after process_file is run all of the variables from that function are destroyed. So you will either need to have process_file return the list of actors (recommended) or have the list of actors be a global variable so you will have access to it throughout your program (not recommended).

You may want to try to put the dictionary code into process_file, to test out if it is a scope problem. If it does work then it then it is a scope problem. If it doesn't, then please post your code again with the dictionary code integrated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.