Skip to main content

Script for converting SWITRS reports to SQLite.

Project description

SWITRS-to-SQLite

Looking to get right to work with the data? Check out the cleaned dataset on Kaggle: California Traffic Collision Data from SWITRS

SWITRS-to-SQLite is a Python 3 script that converts the CSV files from the California Highway Patrol's Statewide Integrated Traffic Records System (SWITRS) into an SQLite database.

Note: CHP shut down iSWITRS on January 8, 2025 and replaced it with the California Crash Reporting System (CCRS). This tool works with the legacy SWITRS CSV format (CollisionRecords.txt, PartyRecords.txt, VictimRecords.txt). CCRS uses a different file layout; support for it is planned for a future release.

Versioning

This repository follows SemVer. I will increment the MAJOR version if a change is backwards incompatible for either the Python command line or the structure of the output database.

Installation

The best way to install SWITRS-to-SQLite is with pip:

pip install switrs-to-sqlite

Or with UV:

uv tool install switrs-to-sqlite

This will give you access to the script simply by calling:

switrs_to_sqlite --help

Development Installation

For development, clone the repository and use UV to sync the environment (this handles the local package installation automatically):

git clone https://github.com/agude/SWITRS-to-SQLite.git
cd SWITRS-to-SQLite
uv sync --dev

SWITRS-to-SQLite requires Python 3.10+.

Usage

Using SWITRS-to-SQLite is simple; just point it to the unzipped files from SWITRS and it will run the conversion:

switrs_to_sqlite \
CollisionRecords.txt \
PartyRecords.txt \
VictimRecords.txt

The script also supports reading gziped records files:

switrs_to_sqlite \
CollisionRecords.txt.gz \
PartyRecords.txt.gz \
VictimRecords.txt.gz

The conversion takes about 10 minutes for a full-sized dump. The database is saved by default to a file named switrs.sqlite3. The output file can be changed as follows:

switrs_to_sqlite \
CollisionRecords.txt \
PartyRecords.txt \
VictimRecords.txt \
-o new_db_file.sqlite3

The program provides the following help menu when called with --help:

usage: switrs_to_sqlite [-h] [--version] [-p {strict,ignore,replace}]
                        [-o OUTPUT_FILE]
                        collision_record party_record victim_record

Convert SWITRS text files to a SQLite3 database

positional arguments:
  collision_record      the CollisionRecords.txt file or the same file gzipped
  party_record          the PartyRecords.txt file or the same file gzipped
  victim_record         the VictimRecords.txt file or the same file gzipped

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -p {strict,ignore,replace}, --parse-error {strict,ignore,replace}
                        how to handle unicode decoding errors in input files:
                        'strict' raises an error (default), 'ignore' skips
                        invalid characters, 'replace' substitutes a
                        replacement character
  -o OUTPUT_FILE, --output-file OUTPUT_FILE
                        file to save the database to

Unit Tests

SWITRS-to-SQLite uses pytest to run unit tests (contained in the tests folder). To run the tests:

uv run pytest -vv

Or with just:

just test

Run just with no arguments to see all available commands.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

switrs_to_sqlite-5.0.0.tar.gz (95.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

switrs_to_sqlite-5.0.0-py3-none-any.whl (37.4 kB view details)

Uploaded Python 3

File details

Details for the file switrs_to_sqlite-5.0.0.tar.gz.

File metadata

  • Download URL: switrs_to_sqlite-5.0.0.tar.gz
  • Upload date:
  • Size: 95.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for switrs_to_sqlite-5.0.0.tar.gz
Algorithm Hash digest
SHA256 208dc10312d1f876fa678200995a216ab142e82bc1d63cdfde60c999966c477b
MD5 4a1c3829c1c1c40721e87974bd5515a6
BLAKE2b-256 63ccee4ac376cbf46c7e2dd753a1bf38c18e42584388353e65136db32cd1a980

See more details on using hashes here.

Provenance

The following attestation bundles were made for switrs_to_sqlite-5.0.0.tar.gz:

Publisher: release.yml on agude/SWITRS-to-SQLite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file switrs_to_sqlite-5.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for switrs_to_sqlite-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c2519adea1511a2d625b1b74f7b08fa66d52b5fa037352e4845a67e86944a0cd
MD5 dee3d8b90092cc7b546a0b461b2ae057
BLAKE2b-256 322a1863df3a18f400ad39a0a6e967f6909403985016050ac056a0f3f8928980

See more details on using hashes here.

Provenance

The following attestation bundles were made for switrs_to_sqlite-5.0.0-py3-none-any.whl:

Publisher: release.yml on agude/SWITRS-to-SQLite

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page