简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM