For a few months now i’ve been doing some CSV Imports in my Liftweb projects. This mostly is because the old databases i am migrating to something normal (PostgreSQL or MongoDB) are badly designed (mostly MySQL) and therefore i don’t even want to bother with their API. So XML seemed a bit heavy for most MySQL-Admins and -Users to work with, so CSV was the next “best” thing.

I have written many CSV Parsers in A LOT of languages, but writing it in Scala felt right for the first time. By the way, i’ve also tried the typical split()-variant, but i wasn’t too happy with it after some time, so i started googling and found this on StackOverflow. But i didn’t like it just yet.

What didn’t fit

  • It is limited to Comma (sometimes you get a Semicolon)
  • It only gives you a List[List[String]], i wanted a List[Map[String, String]]

My version

This will use your first line in the CSV to create Headers, regardless if it has a # in front of it, at the same time it will allow you to access Fields by iterating over the rows and accessing the Map’s index with a Fieldname.

All Fieldnames will be stripped of Spaces!!! Column “First Name” would become “FirstName”.

new CSV(";").parse("foo.csv", "UTF-8")) foreach { row =>

Feedback much appreciated!