问题:我正在阅读一系列异构输入文件。我为它们中的每一个编写了一个阅读器类,它使用 读取文件__init__(self, file_name)
,并在输入格式错误的情况下抛出异常。
代码如下所示:
clients = Clients ('Clients.csv' )
simulation = Simulation ('Simulation.csv' )
indicators = Indicators ('Indicators.csv' )
legalEntity = LegalEntity ('LegalEntity.csv' )
defaultPortfolio = DefaultPortfolio ('DefaultPortfolio.csv' )
excludedProductTypes = ExcludedProductTypes('ExcludedProductTypes.csv')
问题是我不想在第一个格式错误的文件中死去,而是阅读所有文件,如果至少有一个格式错误,则死去。我能找到的唯一方法看起来很可怕:
my errors = []
try:
clients = Clients ('Clients.csv' )
except Exception, e:
errors.append(e)
try:
simulation = Simulation ('Simulation.csv' )
except Exception, e:
errors.append(e)
try:
indicators = Indicators ('Indicators.csv' )
except Exception, e:
errors.append(e)
try:
legalEntity = LegalEntity ('LegalEntity.csv' )
except Exception, e:
errors.append(e)
try:
defaultPortfolio = DefaultPortfolio ('DefaultPortfolio.csv' )
except Exception, e:
errors.append(e)
try:
excludedProductTypes = ExcludedProductTypes('ExcludedProductTypes.csv')
except Exception, e:
errors.append(e)
if len(errors) > 0:
raise MultipleErrors(errors)
有没有更好的方法来解决这个问题?