bw2io.strategies.csv#
Functions#
Add an empty exchanges section to any dictionary in data that doesn't already have one. |
|
|
Remove any keys whose values are (Unknown). |
|
Convert string values to float or int where possible |
|
Convert boolean-like strings to booleans where possible. |
|
Convert tuple-like strings to actual tuples. |
Module Contents#
- 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:
The updated list of dictionaries with an empty exchanges section added to any dictionary that doesnโt already have one.
- Return type:
list[dict]
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:
data (list[dict]) โ A list of dictionaries, where each dictionary represents a row of data.
- Returns:
The updated list of dictionaries with (Unknown) values removed from the keys.
- Return type:
list[dict]
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:
data (list of dict) โ A list of datasets.
- Returns:
A list of datasets with string values converted to float or int where possible.
- Return type:
list of dict
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:
data (list of dict) โ A list of datasets.
- Returns:
A list of datasets with booleans restored.
- Return type:
list of dict
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:
data (list of dict) โ A list of datasets.
- Returns:
A list of datasets with tuples restored from string.
- Return type:
list of dict
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'}]}]