Skip to content

Instantly share code, notes, and snippets.

@cmutel
Last active January 16, 2024 19:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmutel/243fea1edd79b15b998991bfcc737687 to your computer and use it in GitHub Desktop.
Save cmutel/243fea1edd79b15b998991bfcc737687 to your computer and use it in GitHub Desktop.
import csv
import bw2data as bd
bio_db_name = 'ecoinvent-3.10-biosphere'
new_method = ("Something", "Something", "Danger zone")
# Writing
with open(f'{bio_db_name}.csv', 'w', encoding='utf-8') as csvfile:
fieldnames = ['id', 'name', 'categories', 'unit', 'cf']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
flows = sorted(bd.Database(bio_db_name), key=lambda x: (x['name'], x['categories'], x['unit']))
for flow in flows:
writer.writerow({
'id': flow.id,
'name': flow['name'],
'categories': "::".join(flow['categories']),
'unit': flow['unit'],
'cf': 0
})
# Add your CFs to the CSV. You can also start with your own CSV if that's easier.
# Reading
with open(f'{bio_db_name}.csv', encoding='utf-8') as csvfile:
data = [(int(obj['id']), obj['cf']) for obj in csv.DictReader(csvfile)]
my_method = bd.Method(new_method)
my_method.register(unit="foobar")
my_method.write(data)
# Check writing was successful
for flow, cf in bd.Method(new_method).load():
print(bd.get_node(id=flow), cf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment