REGISTER NOW: DdS Autumn School! 🇨🇭 Grosshöchstetten (Switzerland) 🗓️ 6.-11. October 2024

bw2io.strategies.csv#

Module Contents#

Functions#

csv_add_missing_exchanges_section(data)

Add an empty exchanges section to any dictionary in data that doesn't already have one.

csv_drop_unknown(data)

Remove any keys whose values are (Unknown).

csv_numerize(data)

Convert string values to float or int where possible

csv_restore_booleans(data)

Convert boolean-like strings to booleans where possible.

csv_restore_tuples(data)

Convert tuple-like strings to actual tuples.

bw2io.strategies.csv.csv_add_missing_exchanges_section(data)[source]#

Add an empty exchanges section to any dictionary in data that doesn’t already have one.

Parameters#

data: list of dict

A list of dictionaries, where each dictionary represents a row of data.

Returns#

list[dict]

The updated list of dictionaries with an empty exchanges section added to any dictionary that doesn’t already have one.

Examples#

>>> data = [
        {"name": "John", "age": 30},
        {"name": "Alice", "age": 25, "exchanges": []},
        {"name": "Bob", "age": 40, "exchanges": [{"name": "NYSE"}]}
    ]
>>> csv_add_missing_exchanges_section(data)
    [
        {"name": "John", "age": 30, "exchanges": []},
        {"name": "Alice", "age": 25, "exchanges": []},
        {"name": "Bob", "age": 40, "exchanges": [{"name": "NYSE"}]}
    ]
bw2io.strategies.csv.csv_drop_unknown(data)[source]#

Remove any keys whose values are (Unknown).

Parameters#

datalist[dict]

A list of dictionaries, where each dictionary represents a row of data.

Returns#

list[dict]

The updated list of dictionaries with (Unknown) values removed from the keys.

Examples#

>>> data = [
        {"name": "John", "age": 30, "gender": "(Unknown)"},
        {"name": "Alice", "age": 25, "gender": "Female"},
        {"name": "Bob", "age": 40, "gender": "Male"}
    ]
>>> csv_drop_unknown(data)
    [
        {"name": "Alice", "age": 25, "gender": "Female"},
        {"name": "Bob", "age": 40, "gender": "Male"}
    ]
bw2io.strategies.csv.csv_numerize(data)[source]#

Convert string values to float or int where possible

Parameters#

datalist of dict

A list of datasets.

Returns#

list of dict

A list of datasets with string values converted to float or int where possible.

Examples#

>>> data = [{'amount': '10.0'}, {'exchanges': [{'amount': '20', 'uncertainty type': 'undefined'}]}]
>>> csv_numerize(data)
[{'amount': 10.0}, {'exchanges': [{'amount': 20, 'uncertainty type': 'undefined'}]}]
bw2io.strategies.csv.csv_restore_booleans(data)[source]#

Convert boolean-like strings to booleans where possible.

Parameters#

datalist of dict

A list of datasets.

Returns#

list of dict

A list of datasets with booleans restored.

Examples#

>>> data = [{'categories': 'category1', 'is_animal': 'true'}, {'exchanges': [{'categories': 'category2', 'amount': '10.0', 'uncertainty type': 'undefined', 'is_biomass': 'False'}]}]
>>> csv_restore_booleans(data)
[{'categories': 'category1', 'is_animal': True}, {'exchanges': [{'categories': 'category2', 'amount': '10.0', 'uncertainty type': 'undefined', 'is_biomass': False}]}]
bw2io.strategies.csv.csv_restore_tuples(data)[source]#

Convert tuple-like strings to actual tuples.

Parameters#

datalist of dict

A list of datasets.

Returns#

list of dict

A list of datasets with tuples restored from string.

Examples#

>>> data = [{'categories': 'category1::category2'}, {'exchanges': [{'categories': 'category3::category4', 'amount': '10.0'}]}]
>>> csv_restore_tuples(data)
[{'categories': ('category1', 'category2')}, {'exchanges': [{'categories': ('category3', 'category4'), 'amount': '10.0'}]}]