【问题标题】:Brightway2 writing the imported databaseBrightway2 写入导入的数据库
【发布时间】:2021-05-28 21:49:28
【问题描述】:

我使用 eco invent 交换创建了几个 excel 库存文件。 为了运行我的 LCA,我使用以下方法成功导入了包含 0 个未链接交易所的数据库:

imp = bw.ExcelImporter("Inventory_fuelcell.xlsx")
imp.apply_strategies()
imp.match_database("ecoinvent 3.6 cutoff", fields=('name','unit','location'))
imp.match_database("biosphere3", fields=('name','unit'))
imp.match_database(fields=('name', 'unit', 'location'))
imp.statistics()

但是当我运行imp.write_database() 我收到以下错误:

Writing activities to SQLite3 database:
0% [######## ] 100% | ETA: 00:00:00
---------------------------------------------------------------------------
InvalidExchange                           Traceback (most recent call last)
<ipython-input-41-1daab0bbe8d8> in <module>
----> 1 imp.write_database()

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2io/importers/excel.py in write_database(self, **kwargs)
    257         """Same as base ``write_database`` method, but ``activate_parameters`` is True by default."""
    258         kwargs['activate_parameters'] = kwargs.get('activate_parameters', True)
--> 259         super(ExcelImporter, self).write_database(**kwargs)
    260 
    261     def get_activity(self, sn, ws):

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2io/importers/base_lci.py in write_database(self, data, delete_existing, backend, activate_parameters, **kwargs)
    238 
    239         existing.update(data)
--> 240         db.write(existing)
    241 
    242         if activate_parameters:

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2data/project.py in writable_project(wrapped, instance, args, kwargs)
    354     if projects.read_only:
    355         raise ReadOnlyProject(READ_ONLY_PROJECT)
--> 356     return wrapped(*args, **kwargs)

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2data/backends/peewee/database.py in write(self, data, process)
    258         if data:
    259             try:
--> 260                 self._efficient_write_many_data(data)
    261             except:
    262                 # Purge all data from database, then reraise

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2data/backends/peewee/database.py in _efficient_write_many_data(self, data, indices)
    203             for index, (key, ds) in enumerate(data.items()):
    204                 exchanges, activities = self._efficient_write_dataset(
--> 205                     index, key, ds, exchanges, activities
    206                 )
    207 

/opt/anaconda3/envs/Masterarbeit/lib/python3.7/site-packages/bw2data/backends/peewee/database.py in _efficient_write_dataset(self, index, key, ds, exchanges, activities)
    154         for exchange in ds.get('exchanges', []):
    155             if 'input' not in exchange or 'amount' not in exchange:
--> 156                 raise InvalidExchange
    157             if 'type' not in exchange:
    158                 raise UntypedExchange

InvalidExchange: 

我以前从未遇到过这个问题。 有没有办法找出无效交换在哪里? 但是,即使我查找 databases 时出现错误,它仍然会出现。 所以看起来数据库实际上是导入的。 任何人都可以帮助我有什么问题吗?

【问题讨论】:

    标签: python-3.x jupyter-notebook brightway


    【解决方案1】:

    如果我们查看错误回溯,我们可以看到引发错误的行:

        154         for exchange in ds.get('exchanges', []):
        155             if 'input' not in exchange or 'amount' not in exchange:
    --> 156                 raise InvalidExchange
    

    这意味着至少有一个交易所没有inputamount。由于您的所有交换都已链接,它们都具有 input 值,因此必须缺少 amount。这可能是由于列字段中的拼写错误,或因一个错误而关闭等。

    要找到它,您可以尝试:

    for ds in imp.data:
        for exc in ds['exchanges']:
            if 'amount' not in exc:
                print("Missing `amount` in exc:")
                print("\t", exc)
                print("Dataset", ds['name'], ds['location'])
            elif 'input' not in exc:
                # check just to make sure
                print("Missing `input` in exc:")
                print("\t", exc)
                print("Dataset", ds['name'], ds['location'])    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      相关资源
      最近更新 更多