oshribr
oshribr

Reputation: 666

validate csv against schema python

I want to validate csv file against a schema.
I tried to convert the csv to json and to use jsonschema but it doesn't return the specific cells with the error as the csvvalidator. So I thought to use csvvalidator but I can't figure out how to load the schema from the DB.
Any help will be appreciated!

Upvotes: 1

Views: 4959

Answers (1)

Guangyang Li
Guangyang Li

Reputation: 2821

I had similar needs for my stuff, therefore I purposed CSV Schema recently, see https://github.com/csv-schema/csv-schema

I am still working on the documents and more example to make it clear. But basically it allows you writing a JSON as schema to validate CSV files. It borrows lots of keywords from jsonschema, so it might be really easy for you if you have written jsonschema.

I also made a validator in Python https://pypi.python.org/pypi/pycsvschema

Like what's shown in introduction, you can specify your schema like:

schema = {
    'fields': [
        {
            'name': 'value',
            'type': 'number',
            'multipleOf': 5
        }
    ]
}

Then the validator will go through the CSV file, verifying if all value under column "value" are numbers and multiple of 5.

Upvotes: 1

Related Questions