Introducing CodableCSV

CodableCSV is a multi-platform Swift package for parsing/writing CSV files providing:

  • Imperative row-by-row and field-by-field CSV reader/writer.
  • Declarative encoding/decoding through Swift's Codable adoption.
  • Support for multiple inputs/outputs (Strings, Data, File handles, Foundation's Streams, etc.) and Byte Order Markers (BOM).
  • Configurable row/field delimiters, escaping scalars, headers, and trim sets.
  • Configurable buffers supporting huge files (iteratively loading them in memory if necessary).

We just released 0.5.2 finally reaching full Codable adoption. Meaning, you can use CSVDecoder/CSVEncoder and request keyed or unkeyed container to iterate through a CSV file's rows or to iterate through the files within a row.

let decoder = CSVDecoder()
let result = try decoder.decode([Student].self, from: URL("~/Desktop/Students.csv")

Usage and configuration values mimics Foundation's JSON & PLIST strategies including some more such as decimalStrategy (to encode/decode Decimal types from/to String) or bufferStrategy.

Take a look to the README for a deeper explanation or check the Roadmap to see where we are taking it.

9 Likes

I also have my old CommonCoding which includes CSV. The more the merrier :partying_face:.

Hi @Lantua, I actually came across your project just before posting here and it is in my ToDo list to dive deeper in the code. Once I do, I will make sure to put it in the CSV projects comparison section I have in the README.

2 Likes

Fantastic, I recently needed to add a Export-to-CSV functionality in a personal project, so this is great timing. Thanks!

A quick question if I may, does it automatically handle delimiters being present in values?

If anyone is interested in a parser that supports streaming, skelpo/CSV is also an option.

1 Like

does it automatically handle delimiters being present in values?

It does! I am assuming that the field containing the delimiter is escaped with an escaping scalar (commonly a double quote). For examples:

id,name,motto
1,Oscar,"Everything in moderation, including moderation"

If the field wouldn't include the double quotes, it would be interpreted as two fields, which would make the row have 4 fields, which subsequently would throw an error since the previous row has 3 fields and all CSV should have the same amount of fields :smile:

CodableCSV is extremely configurable and you can set the field delimiter, row delimiters and escaping scalar that you want, among other things.

2 Likes

Oh cool. I will check this out too. Thanks for sharing!

I have compiled a small list of CSV imperative and declarative projects at the end of the README in popularity order (Github stars). You can find your projects there @Lantua and @calebkleveter. Hopefully I have been fair to everyone.

2 Likes

I've been using this for a while in a CLI tool we use at work. Very nice :+1:

1 Like

I am getting errors trying to install. Using cocoapods for a macOS project I get:

[!] The platform of the target xxxx (macOS 10.15) is not compatible with CodableCSV (0.4.0), which does not support macOS.

I have updated cocoapods (sudo gem install cocoapods), updated the repos (pod repo update), finally got it working by pointing it directly at your podspec (pod 'CodableCSV', :git => 'https://github.com/dehesa/CodableCSV.git')

Hi @wheeliebin, thank you for reporting. I am already aware of this. I already have a ticket in Github that you can use to monitor the problem completion.

Long story short. I am not a Cocoapods user and the Cocoapods support was performed by a contributor. It seems there is already another package called "CodableCSV", so I need to go and "register". It will be resolve soon. Sorry for the inconvenience.

Terms of Service

Privacy Policy

Cookie Policy