The Best Thing You'll Ever Read About CSVs

Today is [email protected]’s first anniversary. Thank you so much for all your feedback, retweets, and support!

CSV, like email, has persevered as a medium for exchanging data because when nothing else works, it will. But there are tradeoffs. What you gain in compatibility you lose in convenience:

That’s why I got all giddy when I came across smartcsv from Santiago Basulto. It addresses all three issues by parsing CSVs according to a user-defined model.

Here’s an example model for data exported from a school’s student database:

```python model = [ # Define column headers and whether data is required {‘name’: ‘name’, ‘required’: True}, {‘name’: ‘class’, ‘required’: True},

# Validation data based on allowable values { ‘name’: ‘grade’, ‘required’: True, ‘choices’: [‘A’,’B’,’C’,’D’,’F’] },

# Validate data with a function { ‘name’: ‘student_id’, ‘required’: True, ‘validator’: is_real_id },

# Validate data based on string properties { ‘name’: ‘student_portfolio’, ‘required’: False, ‘validator’: lambda c: c.startswith(‘http’) } ] ```

And my metadata? I can add arbitrary information at the top of my files and instruct smartcsv to skip the first X lines when it parses them.

Pretty great right? This is how I felt after reading the docs:

smartcsv got me like
Get smartcsv Discuss it
2017 Neal Shyam