Brightway2使用ExcelImporter调用write_database()时报错TypeError: keywords must be strings的求助
TypeError: keywords must be strings in bw2io ExcelImporter.write_database() Hey there, let's work through this error you're facing. The traceback points directly to db.register(**self.metadata) in the bw2io code, which means the issue lies with non-string keys in your importer's metadata dictionary—Python requires keyword arguments (the **kwargs syntax) to have string keys.
Here are actionable steps to resolve this:
1. Inspect the metadata for non-string keys
First, print out your importer's metadata right before calling write_database() to identify problematic keys:
print(imp.metadata)
Look for any keys that aren't string types (like integers, floats, or even None). For example, a key like 123 instead of "123" would trigger this error.
2. Fix metadata keys manually
If you find non-string keys, convert all keys to strings before writing to the database:
# Convert all metadata keys to strings imp.metadata = {str(key): value for key, value in imp.metadata.items()} # Now try writing again imp.write_database()
3. Verify Excel sheet metadata formatting
Even with the official example, double-check the Excel file's metadata-related sheet (usually named "Metadata" or similar):
- Ensure all cells in the key column are formatted as text (not numbers or blank).
- Make sure there are no empty rows or invalid entries in the metadata section that could be parsed as non-string keys.
4. Update bw2io to the latest version
This could be a known bug that's been fixed in newer releases. Upgrade the package using:
pip install --upgrade bw2io
After upgrading, retry the import and write process with the official example to see if the issue resolves.
内容的提问来源于stack exchange,提问作者JeanS

