简体   繁体   English

如何在整个会话期间保持字典的生命力

[英]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. 我有一个python类文件,正在读取特定文件夹中的所有excel文件。

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. 我的主要入口点来自一个单独的python文件( Main_entry.py ),在该文件中我称为读取类。 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. 同样,如何声明dict,以使数据在每次方法调用时都保持追加。

You could create only one instance of your class ReadExcel and append the proper data at the end of each readexcel call. 您只能创建类ReadExcel一个实例,并在每个readexcel调用的末尾附加适当的数据。 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 并在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. 如果要提供更多信息,如何将文件名传递到脚本中,可能会更容易获得帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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