简体   繁体   中英

How to keep the dictionary alive throughout the session

I have a python class file that is reading all the excel files in a particular folder.

class ReadExcel:

    def readexcel(self, filename):

        # Read excel file
        print("reading excel files ... :", filename)

My main entry point is from a seperate python file ( Main_entry.py ) from where I am calling this read class. I need to send a mail at the end of the execution (ie when all the file has been read and manipulated). My mail body should contain the information like below-:

  File name : File1.xlsx
    Total number of rows= 1
    Number of rows Skipped : 0
    Number of rows inserted  : 0
    Number of rows updated : 1

    File name : File2.xlsx
    Total number of rows= 1
    Number of rows Skipped: 0
    Number of rows inserted  : 0
    Number of rows updated : 1

    File name : File3.xlsx
    Total number of rows= 1
    Number of rows Skipped : 0
    Number of rows inserted : 0
    Number of rows updated : 1

How do I store such a information in my dictionary ? Also, how to declare the dict such that the data keeps on appending at every method call.

You could create only one instance of your class ReadExcel and append the proper data at the end of each readexcel call. Eg:

class ReadExcel:
    def __init__(self):
        self.stats = {}

    def readexcel(self, filename):
        ...
        self.stats[filename] = {"rows": 0, "skipped": 0, ...}

and in the Main_entry.py

...
excel_reader = ReadExcel()
for filename in filenames:
    excel_reader.readexcel(filename)
send_mail(excel_reader.stats)
...

If you would provide more info how are you passing the filenames to your script, it might be easier to help.

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