[Tutor] How to create dictionaries loadable with import

Treder, Robert Robert.Treder at morganstanley.com
Tue Sep 24 22:33:20 CEST 2013


Hi Python tutors,

I'm fairly new to Python.  I'm working with Python v2.7.4 and the nltk package on a couple of text mining projects.  I create several dictionaries that are pretty static. Will probably only be updated every or month or every couple of months.  I want to turn those dictionaries into loadable data sets prior to running a module which uses them.  If I define several dictionaries, dict1, dict2 and dict3, in a single module named myDict, I'd like to do

from myDict import *

I've tried defining the dictionaries in a the myDict module as follows:

Dict1 = {}
with open('file1, 'rb') as infile:
    reader = csv.reader(infile, delimiter = ',')
    for row in reader:
        try:
            Dict1[ row[1] ].append(row[0])
        except:
            Dict1[ row[1] ] = [ row[0], ]

Dict2 = {}
with open('file2, 'rb') as infile:
    reader = csv.reader(infile, delimiter = ',')
    for row in reader:
        try:
            Dict2[ row[1] ].append(row[0])
        except:
            Dict2[ row[1] ] = [ row[0], ]

These are simple dictionary structures with no additional structure, i.e., not embedded in classes or functions.
The try/except sequence is because some of the keys may be duplicated in the files and I want to append the values rather than overwrite.

Now when I build the module with setup tools

python setup.py install -prefix=C:\PY_MODULES

it builds without error but I can't find the dictionaries when I load the module

    from myDict import *
AttributeError: 'module' object has no attribute 'Dict1'

How can I make the dictionaries loadable using import?

Thanks,
Bob




________________________________

NOTICE: Morgan Stanley is not acting as a municipal advisor and the opinions or views contained herein are not intended to be, and do not constitute, advice within the meaning of Section 975 of the Dodd-Frank Wall Street Reform and Consumer Protection Act. If you have received this communication in error, please destroy all electronic and paper copies and notify the sender immediately. Mistransmission is not intended to waive confidentiality or privilege. Morgan Stanley reserves the right, to the extent permitted under applicable law, to monitor electronic communications. This message is subject to terms available at the following link: http://www.morganstanley.com/disclaimers If you cannot access these links, please notify us by reply message and we will send the contents to you. By messaging with Morgan Stanley you consent to the foregoing.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20130924/50d10697/attachment.html>


More information about the Tutor mailing list