defusedcsv is a Python library (version 3.0.0) that acts as a drop-in replacement for the standard library's `csv` module, specifically designed to mitigate CSV injection attacks. It works by sanitizing output, prepending an apostrophe to cells that start with potentially malicious characters like `=`, `+`, `-`, `@`, `|`, or `%`, and escaping `|` characters within these cells. This prevents spreadsheet software (like MS Excel or LibreOffice) from interpreting the cell content as a formula. The library's release cadence appears to be infrequent, with the latest version published to PyPI on September 2, 2025.
pip install defusedcsvVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `defusedcsv` as a drop-in replacement for the standard `csv` module. It shows how potentially malicious spreadsheet formulas are automatically sanitized by prepending an apostrophe and escaping pipe characters, preventing execution when opened in spreadsheet software.
Ensure downstream systems are aware of and can handle the modifications made by `defusedcsv` (prepending apostrophes, escaping pipe characters). If exact original data is required, `defusedcsv` is not suitable.
Ensure your project runs on Python versions 3.9 through 3.13. If you must use other versions, thorough testing is recommended.
Implement comprehensive data validation and sanitization at all input and output points of your application, not relying solely on `defusedcsv` for all security or data integrity concerns.
Install the package using pip: 'pip install defusedcsv'.
Use the correct import: 'import defusedcsv as csv'.
Use 'defusedcsv.reader()' or 'defusedcsv.writer()' instead of calling 'defusedcsv' directly.
Use 'defusedcsv.reader()' and handle rows as dictionaries manually.
Ensure the file is open during the entire read/write operation, possibly using a 'with' statement to manage the file context.
No dependency data recorded yet.