Mocking
Mocking

Reputation: 1894

Importing XML to a dictionary, then accessing that dictionary from another module - Python

I am writing code that reads XML and creates a dictionary. I want to use that dictionary between modules, can I import that generated dictionary to another module?

I thought importing the module would be fine, however since the dictionary is only generated after running the module it is created in, this does not work. Is there any simple way to do this or do I need to write the dictionary to a file and read it again?

Upvotes: 0

Views: 134

Answers (1)

Anthony
Anthony

Reputation: 680

One method you could use is to include a return statement in the module that creates a dict. For instance,

def read_xml():
    dict1 = create_dict_from_xml()
    return dict1

you then could access that dictionary by writing in the other module dict1 = read_xml(). This will only work while the program is running. If you want to save the dict I would recommend using the pickle module. The documentation for that can be fount here. If I didn't answer your question let me know and possibly post some of your source code.

Hope this helped.

Upvotes: 1

Related Questions