The `csvw` Python library (version 3.7.0) provides an API to read and write relational, tabular data in adherence to the W3C CSV on the Web specification. It offers functionalities for parsing CSVW described data, converting it to JSON, and validating metadata. The project maintains an active development status with regular releases.
pip install csvwVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to instantiate a `CSVW` object from a URL pointing to a TSV file (or a CSVW metadata file) and then convert the described data to a JSON representation. The `to_json()` method serializes the tabular data according to the CSVW specification.
Ensure you explicitly `pip install csvw` and `from csvw import CSVW`. Do not confuse it with `csvwlib` which uses `from csvwlib import CSVWConverter`.
If strict positional matching is required, explicitly specify `'header': false` and `'skipRows': 1` in the table's dialect description within your CSVW metadata.
Be aware of these specific `csv` module limitations, particularly when dealing with `commentPrefix`, `escapechar`, `quoteChar`, and `doubleQuote` settings in your dialect. Test data thoroughly with complex characters and quoting.
When working with `anyURI` types, be aware that the string representation may change due to normalization. If exact string preservation is critical for non-normalized URIs, consider storing them as `string` datatype instead, or handle normalization explicitly before passing to `anyURI`.
Install the library using pip: `pip install csvw`
Ensure that the data in columns specified as 'integer' in your CSVW metadata (`.json`) only contains integer values, or adjust the datatype in your metadata to a more flexible type like 'string' or 'decimal' if non-integer data is expected. Alternatively, you might set `strict=False` during CSVW parsing if the library supports it for your specific operation to handle invalid data more gracefully (e.g., by logging warnings instead of raising errors).
Specify the correct encoding when opening or reading the CSV file. If you are using the `csvw` library directly to read, ensure any underlying file-reading mechanisms are provided with the correct encoding. For instance, if reading the CSV first, explicitly set the encoding: `with open('your_file.csv', 'r', encoding='latin-1') as f: ...` or use a tool like `chardet` to detect the encoding if unknown, then apply it.Correct the data in your CSV file to match the datatype defined in your CSVW metadata for that column, or update your CSVW metadata (`.json`) to accurately reflect the actual data type of the column in the CSV file. If parsing programmatically, ensure `validate=True` is set in the `csvw.CSVW` instance to catch these issues during processing.